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 | /** | |
b2c60890 JB |
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. | |
fb6aedc2 JB |
22 | */ |
23 | public function __construct($host, $username, $password, $dbname) | |
24 | { | |
77e90052 | 25 | $this->connection = new mysqli($host, $username, $password, $dbname); |
fb6aedc2 JB |
26 | |
27 | if (!$this->connection->connect_errno) { | |
28 | $this->connected = true; | |
29 | } else { | |
a96fefe1 | 30 | die('Fail to connect to the RDBMS.'); |
fb6aedc2 JB |
31 | } |
32 | ||
33 | return $this->connection; | |
34 | } | |
35 | ||
1f4879b9 JB |
36 | /** |
37 | * [__destruct description] | |
38 | */ | |
a96fefe1 JB |
39 | /* public function __destruct() |
40 | { | |
41 | $this->close(); | |
42 | } */ | |
43 | ||
fb6aedc2 | 44 | /** |
b2c60890 JB |
45 | * close method to close the opened connection to the RDBMS server |
46 | * @return void | |
fb6aedc2 JB |
47 | */ |
48 | public function close() | |
49 | { | |
50 | if ($this->connected && $this->connection->close()) { | |
51 | $this->connected = false; | |
52 | } else { | |
a96fefe1 | 53 | die('Fail to close the connection to the RDBMS.'); |
fb6aedc2 JB |
54 | } |
55 | } | |
56 | ||
57 | /** | |
b2c60890 JB |
58 | * query method that permit to execute a SQL query |
59 | * @param string $sql_query the SQL query to execute | |
60 | * @return mysqli_result object or FALSE on failure and TRUE for queries | |
61 | * without results | |
fb6aedc2 JB |
62 | */ |
63 | public function query($sql_query) | |
64 | { | |
a96fefe1 | 65 | if ($this->connected && !($this->current_result = $this->connection->query($sql_query))) { |
b5f60f89 | 66 | echo "Fail to execute the SQL query : " . $sql_query . "<br>"; |
fb6aedc2 | 67 | } |
a96fefe1 | 68 | return $this->current_result; |
fb6aedc2 JB |
69 | } |
70 | ||
71 | /** | |
b2c60890 JB |
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 | |
fb6aedc2 JB |
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 = ""; | |
b5f60f89 | 82 | echo "Fail to prepare SQL query : (" . $this->connection->errno . ") " . $this->connection->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
83 | } |
84 | return $this->current_stmt; | |
85 | } | |
86 | ||
87 | /** | |
b2c60890 JB |
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 | |
fb6aedc2 | 92 | */ |
a96fefe1 | 93 | public function prepared_query_bind_param($types, $params) |
fb6aedc2 | 94 | { |
a96fefe1 | 95 | $rt_val = $this->current_stmt->bind_param($types, ...$params); |
fb6aedc2 | 96 | if (!$rt_val) { |
b5f60f89 | 97 | echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
98 | } |
99 | return $rt_val; | |
100 | } | |
101 | ||
102 | /** | |
b2c60890 JB |
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 | |
fb6aedc2 JB |
106 | */ |
107 | public function run_prepared_query() | |
108 | { | |
109 | $rt_val = $this->current_stmt->execute(); | |
110 | if (!$rt_val) { | |
b5f60f89 | 111 | echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
112 | } |
113 | return $rt_val; | |
114 | } | |
115 | ||
1f4879b9 JB |
116 | /** |
117 | * [get_pquery_result description] | |
118 | * @return [type] [description] | |
119 | */ | |
a96fefe1 JB |
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 | ||
1f4879b9 JB |
129 | /** |
130 | * [get_result_array description] | |
131 | * @return [type] [description] | |
132 | */ | |
a96fefe1 JB |
133 | public function get_result_array() |
134 | { | |
33eb6f2a JB |
135 | $row = $this->current_result->fetch_array(); |
136 | if (is_null($row)) { | |
137 | $rt_val = []; | |
138 | } elseif (!isset($row)) { | |
a96fefe1 | 139 | echo "Fail to build SQL query result array : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
33eb6f2a JB |
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; | |
a96fefe1 JB |
147 | } |
148 | return $rt_val; | |
149 | } | |
150 | ||
fb6aedc2 JB |
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) { | |
b5f60f89 | 159 | echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
160 | } |
161 | return $rt_val; | |
162 | } | |
163 | } | |
33eb6f2a | 164 | |
fb6aedc2 | 165 | ?> |