| 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 | |
| 13 | /** |
| 14 | * [__construct description] |
| 15 | * @param [type] $host [description] |
| 16 | * @param [type] $username [description] |
| 17 | * @param [type] $password [description] |
| 18 | * @param [type] $dbname [description] |
| 19 | */ |
| 20 | public function __construct($host, $username, $password, $dbname) |
| 21 | { |
| 22 | $connection = new mysqli($host, $username, $password, $dbname); |
| 23 | $this->connection = $connection; |
| 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 | * [close description] |
| 36 | * @return [type] [description] |
| 37 | */ |
| 38 | public function close() |
| 39 | { |
| 40 | if ($this->connected && $this->connection->close()) { |
| 41 | $this->connected = false; |
| 42 | } else { |
| 43 | die('Fail to close the connection to the RDBMS'); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * [query description] |
| 49 | * @param [type] $sql_query [description] |
| 50 | * @return [type] [description] |
| 51 | */ |
| 52 | public function query($sql_query) |
| 53 | { |
| 54 | if ($this->connected && !($query_result = $this->connection->query($sql_query))) { |
| 55 | echo "Fail to execute the SQL query : " . $sql_query; |
| 56 | } |
| 57 | return $query_result; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * [prepare_query description] |
| 62 | * @param [type] $prepared_query [description] |
| 63 | * @return [type] [description] |
| 64 | */ |
| 65 | public function prepare_query($prepared_query) |
| 66 | { |
| 67 | $this->current_pquery = $prepared_query; |
| 68 | if ($this->connected && !($this->current_stmt = $this->connection->prepare($this->current_pquery))) { |
| 69 | // Empty the currently stored prepared query in the failure case |
| 70 | $this->current_pquery = ""; |
| 71 | echo "Fail to prepare SQL query : (" . $this->connection->errno . ") " . $this->connection->error . " - " . $this->current_pquery; |
| 72 | } |
| 73 | return $this->current_stmt; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * [prepared_query_bind_param description] |
| 78 | * @param [type] $params [description] |
| 79 | * @return [type] [description] |
| 80 | */ |
| 81 | public function prepared_query_bind_param(...$params) |
| 82 | { |
| 83 | $rt_val = $this->current_stmt->bind_param($params); |
| 84 | if (!$rt_val) { |
| 85 | echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery; |
| 86 | } |
| 87 | return $rt_val; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * [run_prepared_query description] |
| 92 | * @return [type] [description] |
| 93 | */ |
| 94 | public function run_prepared_query() |
| 95 | { |
| 96 | $rt_val = $this->current_stmt->execute(); |
| 97 | if (!$rt_val) { |
| 98 | echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery; |
| 99 | } |
| 100 | return $rt_val; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * [close_prepared_query description] |
| 105 | * @return [type] [description] |
| 106 | */ |
| 107 | public function close_prepared_query() |
| 108 | { |
| 109 | $rt_val = $this->current_stmt->close(); |
| 110 | if (!$rt_val) { |
| 111 | echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery; |
| 112 | } |
| 113 | return $rt_val; |
| 114 | } |
| 115 | } |
| 116 | ?> |