Code cleanups and comments
[Project_webapp.git] / lib / db.php
index c7c2d48c71f9cb45e756b1151576b7e125e36c1a..9d967d6dd4bbec29ff9d54b1acd9cf6aa33cc9cb 100644 (file)
@@ -12,11 +12,13 @@ class CustomDB
     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)
     {
@@ -31,14 +33,17 @@ class CustomDB
         return $this->connection;
     }
 
+    /**
+     * [__destruct description]
+     */
     /* public function __destruct()
     {
         $this->close();
     } */
 
     /**
-     * [close description]
-     * @return [type] [description]
+     * close method to close the opened connection to the RDBMS server
+     * @return void
      */
     public function close()
     {
@@ -50,9 +55,10 @@ class CustomDB
     }
 
     /**
-     * [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)
     {
@@ -63,9 +69,9 @@ class CustomDB
     }
 
     /**
-     * [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)
     {
@@ -79,9 +85,10 @@ class CustomDB
     }
 
     /**
-     * [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($types, $params)
     {
@@ -93,8 +100,9 @@ class CustomDB
     }
 
     /**
-     * [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()
     {
@@ -105,6 +113,10 @@ class CustomDB
         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();
@@ -114,11 +126,24 @@ class CustomDB
         return $rt_val;
     }
 
+    /**
+     * [get_result_array description]
+     * @return [type] [description]
+     */
     public function get_result_array()
     {
-        $rt_val = $this->current_result->fetch_array();
-        if (!$rt_val) {
+        $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;
     }
@@ -136,4 +161,5 @@ class CustomDB
         return $rt_val;
     }
 }
+
 ?>