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