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 | ||
1f4879b9 JB |
34 | /** |
35 | * [__destruct description] | |
36 | */ | |
a96fefe1 JB |
37 | /* public function __destruct() |
38 | { | |
39 | $this->close(); | |
40 | } */ | |
41 | ||
fb6aedc2 JB |
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 { | |
a96fefe1 | 51 | die('Fail to close the connection to the RDBMS.'); |
fb6aedc2 JB |
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 | { | |
a96fefe1 | 62 | if ($this->connected && !($this->current_result = $this->connection->query($sql_query))) { |
b5f60f89 | 63 | echo "Fail to execute the SQL query : " . $sql_query . "<br>"; |
fb6aedc2 | 64 | } |
a96fefe1 | 65 | return $this->current_result; |
fb6aedc2 JB |
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 = ""; | |
b5f60f89 | 79 | echo "Fail to prepare SQL query : (" . $this->connection->errno . ") " . $this->connection->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
80 | } |
81 | return $this->current_stmt; | |
82 | } | |
83 | ||
84 | /** | |
85 | * [prepared_query_bind_param description] | |
33eb6f2a | 86 | * @param [type] $types [description] |
fb6aedc2 JB |
87 | * @param [type] $params [description] |
88 | * @return [type] [description] | |
89 | */ | |
a96fefe1 | 90 | public function prepared_query_bind_param($types, $params) |
fb6aedc2 | 91 | { |
a96fefe1 | 92 | $rt_val = $this->current_stmt->bind_param($types, ...$params); |
fb6aedc2 | 93 | if (!$rt_val) { |
b5f60f89 | 94 | echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
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) { | |
b5f60f89 | 107 | echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
108 | } |
109 | return $rt_val; | |
110 | } | |
111 | ||
1f4879b9 JB |
112 | /** |
113 | * [get_pquery_result description] | |
114 | * @return [type] [description] | |
115 | */ | |
a96fefe1 JB |
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 | ||
1f4879b9 JB |
125 | /** |
126 | * [get_result_array description] | |
127 | * @return [type] [description] | |
128 | */ | |
a96fefe1 JB |
129 | public function get_result_array() |
130 | { | |
33eb6f2a JB |
131 | $row = $this->current_result->fetch_array(); |
132 | if (is_null($row)) { | |
133 | $rt_val = []; | |
134 | } elseif (!isset($row)) { | |
a96fefe1 | 135 | echo "Fail to build SQL query result array : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
33eb6f2a JB |
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; | |
a96fefe1 JB |
143 | } |
144 | return $rt_val; | |
145 | } | |
146 | ||
fb6aedc2 JB |
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) { | |
b5f60f89 | 155 | echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
156 | } |
157 | return $rt_val; | |
158 | } | |
159 | } | |
33eb6f2a | 160 | |
fb6aedc2 | 161 | ?> |