Code cleanups and comments
[Project_webapp.git] / lib / db.php
index 0f280a459e8efbdbeb55c0de5730481678b6cf9b..9d967d6dd4bbec29ff9d54b1acd9cf6aa33cc9cb 100644 (file)
@@ -9,58 +9,69 @@ class CustomDB
     public $connected;
     private $current_pquery;
     private $current_stmt;
+    private $current_result;
 
     /**
-     * [__construct description]
-     * @param [type] $host     [description]
-     * @param [type] $username [description]
-     * @param [type] $password [description]
-     * @param [type] $dbname   [description]
+     * __construct class constructor method that permit to initialize
+     * the connection to the RDBMS database
+     * @param string $host     RDBMS hostname
+     * @param string $username RDBMS user
+     * @param string $password RDBMS user password
+     * @param string $dbname   RDBMS database to use
+     * @return object an object which represents the connection to a RDBMS Server.
      */
     public function __construct($host, $username, $password, $dbname)
     {
-        $connection = new mysqli($host, $username, $password, $dbname);
-        $this->connection = $connection;
+        $this->connection = new mysqli($host, $username, $password, $dbname);
 
         if (!$this->connection->connect_errno) {
             $this->connected = true;
         } else {
-            die('Fail to connect to the RDBMS');
+            die('Fail to connect to the RDBMS.');
         }
 
         return $this->connection;
     }
 
     /**
-     * [close description]
-     * @return [type] [description]
+     * [__destruct description]
+     */
+    /* public function __destruct()
+    {
+        $this->close();
+    } */
+
+    /**
+     * close method to close the opened connection to the RDBMS server
+     * @return void
      */
     public function close()
     {
         if ($this->connected && $this->connection->close()) {
             $this->connected = false;
         } else {
-            die('Fail to close the connection to the RDBMS');
+            die('Fail to close the connection to the RDBMS.');
         }
     }
 
     /**
-     * [query description]
-     * @param  [type] $sql_query [description]
-     * @return [type]            [description]
+     * query method that permit to execute a SQL query
+     * @param  string $sql_query the SQL query to execute
+     * @return object mysqli_result or FALSE on failure and TRUE for queries
+     * without results
      */
     public function query($sql_query)
     {
-        if ($this->connected && !($query_result = $this->connection->query($sql_query))) {
-            echo "Fail to execute the SQL query : " . $sql_query;
+        if ($this->connected && !($this->current_result = $this->connection->query($sql_query))) {
+            echo "Fail to execute the SQL query : " . $sql_query . "<br>";
         }
-        return $query_result;
+        return $this->current_result;
     }
 
     /**
-     * [prepare_query description]
-     * @param  [type] $prepared_query [description]
-     * @return [type]                 [description]
+     * prepare_query method for parametrized SQL query preparation
+     * @param  string $prepared_query parametrized SQL query to prepare
+     * @return object statement object or FALSE if an error occurred
      */
     public function prepare_query($prepared_query)
     {
@@ -68,34 +79,71 @@ class CustomDB
         if ($this->connected && !($this->current_stmt = $this->connection->prepare($this->current_pquery))) {
             // Empty the currently stored prepared query in the failure case
             $this->current_pquery = "";
-            echo "Fail to prepare SQL query : (" . $this->connection->errno . ") " . $this->connection->error . " - " . $this->current_pquery;
+            echo "Fail to prepare SQL query : (" . $this->connection->errno . ") " . $this->connection->error . " - " . $this->current_pquery . "<br>";
         }
         return $this->current_stmt;
     }
 
     /**
-     * [prepared_query_bind_param description]
-     * @param  [type] $params [description]
-     * @return [type]         [description]
+     * prepared_query_bind_param link parameters values to a prepared SQL query
+     * @param  string $types  parameters types
+     * @param  array $params unidimensional array of parameters values
+     * @return boolean TRUE on success or FALSE on failure
      */
-    public function prepared_query_bind_param(...$params)
+    public function prepared_query_bind_param($types, $params)
     {
-        $rt_val = $this->current_stmt->bind_param($params);
+        $rt_val = $this->current_stmt->bind_param($types, ...$params);
         if (!$rt_val) {
-            echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery;
+            echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
         }
         return $rt_val;
     }
 
     /**
-     * [run_prepared_query description]
-     * @return [type] [description]
+     * run_prepared_query method that execute a parametrized SQL query linked
+     * with its parameters values
+     * @return boolean TRUE on success or FALSE on failure
      */
     public function run_prepared_query()
     {
         $rt_val = $this->current_stmt->execute();
         if (!$rt_val) {
-            echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery;
+            echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
+        }
+        return $rt_val;
+    }
+
+    /**
+     * get_pquery_result method to get the resultset of parametrized SQL query
+     * @return object mysqli_result resultset or FALSE for other DML queries or on failure
+     */
+    public function get_pquery_result()
+    {
+        $rt_val = $this->current_result = $this->current_stmt->get_result();
+        if (!$rt_val) {
+            echo "Fail to fill SQL query result : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
+        }
+        return $rt_val;
+    }
+
+    /**
+     * [get_result_array description]
+     * @return [type] [description]
+     */
+    public function get_result_array()
+    {
+        $row = $this->current_result->fetch_array();
+        if (is_null($row)) {
+            $rt_val = [];
+        } elseif (!isset($row)) {
+            echo "Fail to build SQL query result array : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
+            $rt_val = false;
+        } else {
+            $rows[] = $row;
+            while ($row = $this->current_result->fetch_array()) {
+                $rows[] = $row;
+            }
+            $rt_val = $rows;
         }
         return $rt_val;
     }
@@ -108,9 +156,10 @@ class CustomDB
     {
         $rt_val = $this->current_stmt->close();
         if (!$rt_val) {
-            echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery;
+            echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
         }
         return $rt_val;
     }
 }
+
 ?>