4 * [CustomDB description]
10 private $current_pquery;
11 private $current_stmt;
12 private $current_result;
15 * __construct class constructor method that permit to initialize
16 * the connection to the RDBMS database
17 * @param string $host RDBMS hostname
18 * @param string $username RDBMS user
19 * @param string $password RDBMS user password
20 * @param string $dbname RDBMS database to use
21 * @return object an object which represents the connection to a RDBMS Server.
23 public function __construct($host, $username, $password, $dbname)
25 $this->connection
= new mysqli($host, $username, $password, $dbname);
27 if (!$this->connection
->connect_errno
) {
28 $this->connected
= true;
30 die('Fail to connect to the RDBMS.');
33 return $this->connection
;
37 * [__destruct description]
39 /* public function __destruct()
45 * close method to close the opened connection to the RDBMS server
48 public function close()
50 if ($this->connected
&& $this->connection
->close()) {
51 $this->connected
= false;
53 die('Fail to close the connection to the RDBMS.');
58 * query method that permit to execute a SQL query
59 * @param string $sql_query the SQL query to execute
60 * @return object mysqli_result or FALSE on failure and TRUE for queries
63 public function query($sql_query)
65 if ($this->connected
&& !($this->current_result
= $this->connection
->query($sql_query))) {
66 echo "Fail to execute the SQL query : " . $sql_query . "<br>";
68 return $this->current_result
;
72 * prepare_query method for parametrized SQL query preparation
73 * @param string $prepared_query parametrized SQL query to prepare
74 * @return object statement object or FALSE if an error occurred
76 public function prepare_query($prepared_query)
78 $this->current_pquery
= $prepared_query;
79 if ($this->connected
&& !($this->current_stmt
= $this->connection
->prepare($this->current_pquery
))) {
80 // Empty the currently stored prepared query in the failure case
81 $this->current_pquery
= "";
82 echo "Fail to prepare SQL query : (" . $this->connection
->errno
. ") " . $this->connection
->error
. " - " . $this->current_pquery
. "<br>";
84 return $this->current_stmt
;
88 * prepared_query_bind_param link parameters values to a prepared SQL query
89 * @param string $types parameters types
90 * @param array $params unidimensional array of parameters values
91 * @return boolean TRUE on success or FALSE on failure
93 public function prepared_query_bind_param($types, $params)
95 $rt_val = $this->current_stmt
->bind_param($types, ...$params);
97 echo "Fail to link parameters to SQL query : (" . $this->current_stmt
->errno
. ") " . $this->current_stmt
->error
. " - " . $this->current_pquery
. "<br>";
103 * run_prepared_query method that execute a parametrized SQL query linked
104 * with its parameters values
105 * @return boolean TRUE on success or FALSE on failure
107 public function run_prepared_query()
109 $rt_val = $this->current_stmt
->execute();
111 echo "Fail to execute SQL query : (" . $this->current_stmt
->errno
. ") " . $this->current_stmt
->error
. " - " . $this->current_pquery
. "<br>";
117 * get_pquery_result method to get the resultset of parametrized SQL query
118 * @return object mysqli_result resultset or FALSE for other DML queries or on failure
120 public function get_pquery_result()
122 $rt_val = $this->current_result
= $this->current_stmt
->get_result();
124 echo "Fail to fill SQL query result : (" . $this->current_stmt
->errno
. ") " . $this->current_stmt
->error
. " - " . $this->current_pquery
. "<br>";
130 * [get_result_array description]
131 * @return [type] [description]
133 public function get_result_array()
135 $row = $this->current_result
->fetch_array();
138 } elseif (!isset($row)) {
139 echo "Fail to build SQL query result array : (" . $this->current_stmt
->errno
. ") " . $this->current_stmt
->error
. " - " . $this->current_pquery
. "<br>";
143 while ($row = $this->current_result
->fetch_array()) {
152 * [close_prepared_query description]
153 * @return [type] [description]
155 public function close_prepared_query()
157 $rt_val = $this->current_stmt
->close();
159 echo "Fail to close SQL query : (" . $this->current_stmt
->errno
. ") " . $this->current_stmt
->error
. " - " . $this->current_pquery
. "<br>";