Properly separate the HTTP GET requests from the POST requests
[Project_webapp.git] / lib / db.php
diff --git a/lib/db.php b/lib/db.php
new file mode 100644 (file)
index 0000000..0f280a4
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+
+/**
+ * [CustomDB description]
+ */
+class CustomDB
+{
+    private $connection;
+    public $connected;
+    private $current_pquery;
+    private $current_stmt;
+
+    /**
+     * [__construct description]
+     * @param [type] $host     [description]
+     * @param [type] $username [description]
+     * @param [type] $password [description]
+     * @param [type] $dbname   [description]
+     */
+    public function __construct($host, $username, $password, $dbname)
+    {
+        $connection = new mysqli($host, $username, $password, $dbname);
+        $this->connection = $connection;
+
+        if (!$this->connection->connect_errno) {
+            $this->connected = true;
+        } else {
+            die('Fail to connect to the RDBMS');
+        }
+
+        return $this->connection;
+    }
+
+    /**
+     * [close description]
+     * @return [type] [description]
+     */
+    public function close()
+    {
+        if ($this->connected && $this->connection->close()) {
+            $this->connected = false;
+        } else {
+            die('Fail to close the connection to the RDBMS');
+        }
+    }
+
+    /**
+     * [query description]
+     * @param  [type] $sql_query [description]
+     * @return [type]            [description]
+     */
+    public function query($sql_query)
+    {
+        if ($this->connected && !($query_result = $this->connection->query($sql_query))) {
+            echo "Fail to execute the SQL query : " . $sql_query;
+        }
+        return $query_result;
+    }
+
+    /**
+     * [prepare_query description]
+     * @param  [type] $prepared_query [description]
+     * @return [type]                 [description]
+     */
+    public function prepare_query($prepared_query)
+    {
+        $this->current_pquery = $prepared_query;
+        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;
+        }
+        return $this->current_stmt;
+    }
+
+    /**
+     * [prepared_query_bind_param description]
+     * @param  [type] $params [description]
+     * @return [type]         [description]
+     */
+    public function prepared_query_bind_param(...$params)
+    {
+        $rt_val = $this->current_stmt->bind_param($params);
+        if (!$rt_val) {
+            echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery;
+        }
+        return $rt_val;
+    }
+
+    /**
+     * [run_prepared_query description]
+     * @return [type] [description]
+     */
+    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;
+        }
+        return $rt_val;
+    }
+
+    /**
+     * [close_prepared_query description]
+     * @return [type] [description]
+     */
+    public function close_prepared_query()
+    {
+        $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;
+        }
+        return $rt_val;
+    }
+}
+?>