Misc code cleanups and comment.
[Project_webapp.git] / lib / db.php
index b548804c385f586ede81f6ac8f5c73f875d8dc92..bcbf5aebc2709265decce8a7d53ac86099f190f5 100644 (file)
@@ -9,6 +9,7 @@ class CustomDB
     public $connected;
     private $current_pquery;
     private $current_stmt;
+    private $current_result;
 
     /**
      * [__construct description]
@@ -19,18 +20,25 @@ class CustomDB
      */
     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;
     }
 
+    /**
+     * [__destruct description]
+     */
+    /* public function __destruct()
+    {
+        $this->close();
+    } */
+
     /**
      * [close description]
      * @return [type] [description]
@@ -40,7 +48,7 @@ class CustomDB
         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.');
         }
     }
 
@@ -51,10 +59,10 @@ class CustomDB
      */
     public function query($sql_query)
     {
-        if ($this->connected && !($query_result = $this->connection->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;
     }
 
     /**
@@ -75,13 +83,13 @@ class CustomDB
 
     /**
      * [prepared_query_bind_param description]
+     * @param  [type] $types  [description]
      * @param  [type] $params [description]
      * @return [type]         [description]
      */
-    public function prepared_query_bind_param(...$params)
+    public function prepared_query_bind_param($types, $params)
     {
-        var_dump($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 . "<br>";
         }
@@ -101,6 +109,41 @@ class CustomDB
         return $rt_val;
     }
 
+    /**
+     * [get_pquery_result description]
+     * @return [type] [description]
+     */
+    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;
+    }
+
     /**
      * [close_prepared_query description]
      * @return [type] [description]
@@ -114,4 +157,5 @@ class CustomDB
         return $rt_val;
     }
 }
+
 ?>