| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * [CustomDB description] |
| 5 | */ |
| 6 | class CustomDB |
| 7 | { |
| 8 | private $connection; |
| 9 | public $connected; |
| 10 | private $current_pquery; |
| 11 | private $current_stmt; |
| 12 | private $current_result; |
| 13 | |
| 14 | /** |
| 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. |
| 22 | */ |
| 23 | public function __construct($host, $username, $password, $dbname) |
| 24 | { |
| 25 | $this->connection = new mysqli($host, $username, $password, $dbname); |
| 26 | |
| 27 | if (!$this->connection->connect_errno) { |
| 28 | $this->connected = true; |
| 29 | } else { |
| 30 | die('Fail to connect to the RDBMS.'); |
| 31 | } |
| 32 | |
| 33 | return $this->connection; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * [__destruct description] |
| 38 | */ |
| 39 | /* public function __destruct() |
| 40 | { |
| 41 | $this->close(); |
| 42 | } */ |
| 43 | |
| 44 | /** |
| 45 | * close method to close the opened connection to the RDBMS server |
| 46 | * @return void |
| 47 | */ |
| 48 | public function close() |
| 49 | { |
| 50 | if ($this->connected && $this->connection->close()) { |
| 51 | $this->connected = false; |
| 52 | } else { |
| 53 | die('Fail to close the connection to the RDBMS.'); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 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 |
| 61 | * without results |
| 62 | */ |
| 63 | public function query($sql_query) |
| 64 | { |
| 65 | if ($this->connected && !($this->current_result = $this->connection->query($sql_query))) { |
| 66 | echo "Fail to execute the SQL query : " . $sql_query . "<br>"; |
| 67 | } |
| 68 | return $this->current_result; |
| 69 | } |
| 70 | |
| 71 | /** |
| 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 |
| 75 | */ |
| 76 | public function prepare_query($prepared_query) |
| 77 | { |
| 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>"; |
| 83 | } |
| 84 | return $this->current_stmt; |
| 85 | } |
| 86 | |
| 87 | /** |
| 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 |
| 92 | */ |
| 93 | public function prepared_query_bind_param($types, $params) |
| 94 | { |
| 95 | $rt_val = $this->current_stmt->bind_param($types, ...$params); |
| 96 | if (!$rt_val) { |
| 97 | echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 98 | } |
| 99 | return $rt_val; |
| 100 | } |
| 101 | |
| 102 | /** |
| 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 |
| 106 | */ |
| 107 | public function run_prepared_query() |
| 108 | { |
| 109 | $rt_val = $this->current_stmt->execute(); |
| 110 | if (!$rt_val) { |
| 111 | echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 112 | } |
| 113 | return $rt_val; |
| 114 | } |
| 115 | |
| 116 | /** |
| 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 |
| 119 | */ |
| 120 | public function get_pquery_result() |
| 121 | { |
| 122 | $rt_val = $this->current_result = $this->current_stmt->get_result(); |
| 123 | if (!$rt_val) { |
| 124 | echo "Fail to fill SQL query result : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 125 | } |
| 126 | return $rt_val; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * [get_result_array description] |
| 131 | * @return [type] [description] |
| 132 | */ |
| 133 | public function get_result_array() |
| 134 | { |
| 135 | $row = $this->current_result->fetch_array(); |
| 136 | if (is_null($row)) { |
| 137 | $rt_val = []; |
| 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>"; |
| 140 | $rt_val = false; |
| 141 | } else { |
| 142 | $rows[] = $row; |
| 143 | while ($row = $this->current_result->fetch_array()) { |
| 144 | $rows[] = $row; |
| 145 | } |
| 146 | $rt_val = $rows; |
| 147 | } |
| 148 | return $rt_val; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * [close_prepared_query description] |
| 153 | * @return [type] [description] |
| 154 | */ |
| 155 | public function close_prepared_query() |
| 156 | { |
| 157 | $rt_val = $this->current_stmt->close(); |
| 158 | if (!$rt_val) { |
| 159 | echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 160 | } |
| 161 | return $rt_val; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | ?> |