| 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 description] |
| 16 | * @param [type] $host [description] |
| 17 | * @param [type] $username [description] |
| 18 | * @param [type] $password [description] |
| 19 | * @param [type] $dbname [description] |
| 20 | */ |
| 21 | public function __construct($host, $username, $password, $dbname) |
| 22 | { |
| 23 | $this->connection = new mysqli($host, $username, $password, $dbname); |
| 24 | |
| 25 | if (!$this->connection->connect_errno) { |
| 26 | $this->connected = true; |
| 27 | } else { |
| 28 | die('Fail to connect to the RDBMS.'); |
| 29 | } |
| 30 | |
| 31 | return $this->connection; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * [__destruct description] |
| 36 | */ |
| 37 | /* public function __destruct() |
| 38 | { |
| 39 | $this->close(); |
| 40 | } */ |
| 41 | |
| 42 | /** |
| 43 | * [close description] |
| 44 | * @return [type] [description] |
| 45 | */ |
| 46 | public function close() |
| 47 | { |
| 48 | if ($this->connected && $this->connection->close()) { |
| 49 | $this->connected = false; |
| 50 | } else { |
| 51 | die('Fail to close the connection to the RDBMS.'); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * [query description] |
| 57 | * @param [type] $sql_query [description] |
| 58 | * @return [type] [description] |
| 59 | */ |
| 60 | public function query($sql_query) |
| 61 | { |
| 62 | if ($this->connected && !($this->current_result = $this->connection->query($sql_query))) { |
| 63 | echo "Fail to execute the SQL query : " . $sql_query . "<br>"; |
| 64 | } |
| 65 | return $this->current_result; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * [prepare_query description] |
| 70 | * @param [type] $prepared_query [description] |
| 71 | * @return [type] [description] |
| 72 | */ |
| 73 | public function prepare_query($prepared_query) |
| 74 | { |
| 75 | $this->current_pquery = $prepared_query; |
| 76 | if ($this->connected && !($this->current_stmt = $this->connection->prepare($this->current_pquery))) { |
| 77 | // Empty the currently stored prepared query in the failure case |
| 78 | $this->current_pquery = ""; |
| 79 | echo "Fail to prepare SQL query : (" . $this->connection->errno . ") " . $this->connection->error . " - " . $this->current_pquery . "<br>"; |
| 80 | } |
| 81 | return $this->current_stmt; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * [prepared_query_bind_param description] |
| 86 | * @param [type] $types [description] |
| 87 | * @param [type] $params [description] |
| 88 | * @return [type] [description] |
| 89 | */ |
| 90 | public function prepared_query_bind_param($types, $params) |
| 91 | { |
| 92 | $rt_val = $this->current_stmt->bind_param($types, ...$params); |
| 93 | if (!$rt_val) { |
| 94 | echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 95 | } |
| 96 | return $rt_val; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * [run_prepared_query description] |
| 101 | * @return [type] [description] |
| 102 | */ |
| 103 | public function run_prepared_query() |
| 104 | { |
| 105 | $rt_val = $this->current_stmt->execute(); |
| 106 | if (!$rt_val) { |
| 107 | echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 108 | } |
| 109 | return $rt_val; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * [get_pquery_result description] |
| 114 | * @return [type] [description] |
| 115 | */ |
| 116 | public function get_pquery_result() |
| 117 | { |
| 118 | $rt_val = $this->current_result = $this->current_stmt->get_result(); |
| 119 | if (!$rt_val) { |
| 120 | echo "Fail to fill SQL query result : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 121 | } |
| 122 | return $rt_val; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * [get_result_array description] |
| 127 | * @return [type] [description] |
| 128 | */ |
| 129 | public function get_result_array() |
| 130 | { |
| 131 | $row = $this->current_result->fetch_array(); |
| 132 | if (is_null($row)) { |
| 133 | $rt_val = []; |
| 134 | } elseif (!isset($row)) { |
| 135 | echo "Fail to build SQL query result array : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 136 | $rt_val = false; |
| 137 | } else { |
| 138 | $rows[] = $row; |
| 139 | while ($row = $this->current_result->fetch_array()) { |
| 140 | $rows[] = $row; |
| 141 | } |
| 142 | $rt_val = $rows; |
| 143 | } |
| 144 | return $rt_val; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * [close_prepared_query description] |
| 149 | * @return [type] [description] |
| 150 | */ |
| 151 | public function close_prepared_query() |
| 152 | { |
| 153 | $rt_val = $this->current_stmt->close(); |
| 154 | if (!$rt_val) { |
| 155 | echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
| 156 | } |
| 157 | return $rt_val; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | ?> |