]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/ezSQL/ez_sql_mysqli.php
Code cleanup & new log message queue
[Github/YOURLS.git] / includes / ezSQL / ez_sql_mysqli.php
1 <?php
2
3         /**********************************************************************
4         *  Author: Juergen Bouché (jbouche@nurfuerspam.de)
5         *  Web...: http://www.juergenbouche.de
6         *  Name..: ezSQL_mysqli
7         *  Desc..: mySQLi component (part of ezSQL databse abstraction library)
8         *
9         */
10
11         /**********************************************************************
12         *  ezSQL error strings - mySQLi
13         */
14
15         $ezsql_mysqli_str = array
16         (
17                 1 => 'Require $dbuser and $dbpassword to connect to a database server',
18                 2 => 'Error establishing mySQLi database connection. Correct user/password? Correct hostname? Database server running?',
19                 3 => 'Require $dbname to select a database',
20                 4 => 'mySQLi database connection is not active',
21                 5 => 'Unexpected error while trying to select database'
22         );
23
24         /**********************************************************************
25         *  ezSQL Database specific class - mySQLi
26         */
27
28         if ( ! function_exists ('mysqli_connect') ) die('<b>Fatal Error:</b> ezSQL_mysql requires mySQLi Lib to be compiled and or linked in to the PHP engine');
29         if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
30
31         class ezSQL_mysqli extends ezSQLcore
32         {
33
34                 var $dbuser = false;
35                 var $dbpassword = false;
36                 var $dbname = false;
37                 var $dbhost = false;
38                 var $encoding = false;
39
40                 /**********************************************************************
41                 *  Constructor - allow the user to perform a qucik connect at the
42                 *  same time as initialising the ezSQL_mysqli class
43                 */
44
45                 function ezSQL_mysqli($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $encoding='')
46                 {
47                         $this->dbuser = $dbuser;
48                         $this->dbpassword = $dbpassword;
49                         $this->dbname = $dbname;
50                         $this->dbhost = $dbhost;
51                         $this->encoding = $encoding;
52                 }
53
54                 /**********************************************************************
55                 *  Short hand way to connect to mySQL database server
56                 *  and select a mySQL database at the same time
57                 */
58
59                 function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $encoding='')
60                 {
61                         $return_val = false;
62                         if ( ! $this->connect($dbuser, $dbpassword, $dbhost,true) ) ;
63                         else if ( ! $this->select($dbname,$encoding) ) ;
64                         else $return_val = true;
65                         return $return_val;
66                 }
67
68                 /**********************************************************************
69                 *  Try to connect to mySQL database server
70                 */
71
72                 function connect($dbuser='', $dbpassword='', $dbhost='localhost')
73                 {
74                         global $ezsql_mysqli_str; $return_val = false;
75                         
76                         // Keep track of how long the DB takes to connect
77                         $this->timer_start('db_connect_time');
78
79                         // Must have a user and a password
80                         if ( ! $dbuser )
81                         {
82                                 $this->register_error($ezsql_mysqli_str[1].' in '.__FILE__.' on line '.__LINE__);
83                                 $this->show_errors ? trigger_error($ezsql_mysqli_str[1],E_USER_WARNING) : null;
84                         }
85                         // Try to establish the server database handle
86                         else if ( ! $this->dbh = new mysqli($dbhost,$dbuser,$dbpassword) )
87                         {
88                                 $this->register_error($ezsql_mysqli_str[2].' in '.__FILE__.' on line '.__LINE__);
89                                 $this->show_errors ? trigger_error($ezsql_mysqli_str[2],E_USER_WARNING) : null;
90                         }
91                         else
92                         {
93                                 $this->dbuser = $dbuser;
94                                 $this->dbpassword = $dbpassword;
95                                 $this->dbhost = $dbhost;
96                                 $return_val = true;
97                         }
98
99                         return $return_val;
100                 }
101
102                 /**********************************************************************
103                 *  Try to select a mySQL database
104                 */
105
106                 function select($dbname='', $encoding='')
107                 {
108                         global $ezsql_mysqli_str; $return_val = false;
109
110                         // Must have a database name
111                         if ( ! $dbname )
112                         {
113                                 $this->register_error($ezsql_mysqli_str[3].' in '.__FILE__.' on line '.__LINE__);
114                                 $this->show_errors ? trigger_error($ezsql_mysqli_str[3],E_USER_WARNING) : null;
115                         }
116
117                         // Must have an active database connection
118                         else if ( ! $this->dbh )
119                         {
120                                 $this->register_error($ezsql_mysqli_str[4].' in '.__FILE__.' on line '.__LINE__);
121                                 $this->show_errors ? trigger_error($ezsql_mysqli_str[4],E_USER_WARNING) : null;
122                         }
123
124                         // Try to connect to the database
125                         else if ( !@$this->dbh->select_db($dbname) )
126                         {
127                                 // Try to get error supplied by mysql if not use our own
128                                 if ( !$str = @$this->dbh->error)
129                                           $str = $ezsql_mysqli_str[5];
130
131                                 $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
132                                 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
133                         }
134                         else
135                         {
136                                 $this->dbname = $dbname;
137                                 if($encoding!='')
138                                 {
139                                         $encoding = strtolower(str_replace("-","",$encoding));
140                                         $charsets = array();
141                                         $result = $this->dbh->query("SHOW CHARACTER SET");
142                                         while($row = $result->fetch_array(MYSQLI_ASSOC))
143                                         {
144                                                 $charsets[] = $row["Charset"];
145                                         }
146                                         if(in_array($encoding,$charsets)){
147                                                 $this->dbh->query("SET NAMES '".$encoding."'");                                         
148                                         }
149                                 }
150                                 
151                                 $return_val = true;
152                         }
153
154                         return $return_val;
155                 }
156
157                 /**********************************************************************
158                 *  Format a mySQL string correctly for safe mySQL insert
159                 *  (no mater if magic quotes are on or not)
160                 */
161
162                 function escape($str)
163                 {
164                         // If there is no existing database connection then try to connect
165                         if ( ! isset($this->dbh) || ! $this->dbh )
166                         {
167                                 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
168                                 $this->select($this->dbname, $this->encoding);
169                         }
170
171                         return $this->dbh->escape_string(stripslashes($str));
172                 }
173
174                 /**********************************************************************
175                 *  Return mySQL specific system date syntax
176                 *  i.e. Oracle: SYSDATE Mysql: NOW()
177                 */
178
179                 function sysdate()
180                 {
181                         return 'NOW()';
182                 }
183
184                 /**********************************************************************
185                 *  Perform mySQL query and try to determine result value
186                 */
187
188                 function query($query)
189                 {
190
191                         // This keeps the connection alive for very long running scripts
192                         if ( $this->num_queries >= 500 )
193                         {
194                                 $this->disconnect();
195                                 $this->quick_connect($this->dbuser,$this->dbpassword,$this->dbname,$this->dbhost,$this->encoding);
196                         }
197
198                         // Initialise return
199                         $return_val = 0;
200
201                         // Flush cached values..
202                         $this->flush();
203
204                         // For reg expressions
205                         $query = trim($query);
206
207                         // Log how the function was called
208                         $this->func_call = "\$db->query(\"$query\")";
209
210                         // Keep track of the last query for debug..
211                         $this->last_query = $query;
212
213                         // Count how many queries there have been
214                         $this->num_queries++;
215                         
216                         // Start timer
217                         $this->timer_start($this->num_queries);
218
219                         // Use core file cache function
220                         if ( $cache = $this->get_cache($query) )
221                         {
222                                 // Keep tack of how long all queries have taken
223                                 $this->timer_update_global($this->num_queries);
224
225                                 // Trace all queries
226                                 if ( $this->use_trace_log )
227                                 {
228                                         $this->trace_log[] = $this->debug(false);
229                                 }
230                                 
231                                 return $cache;
232                         }
233
234                         // If there is no existing database connection then try to connect
235                         if ( ! isset($this->dbh) || ! $this->dbh )
236                         {
237                                 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
238                                 $this->select($this->dbname,$this->encoding);
239                         }
240
241                         // Perform the query via std mysql_query function..
242                         $this->result = @$this->dbh->query($query);
243
244                         // If there is an error then take note of it..
245                         if ( $str = @$this->dbh->error )
246                         {
247                                 $is_insert = true;
248                                 $this->register_error($str);
249                                 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
250                                 return false;
251                         }
252
253                         // Query was an insert, delete, update, replace
254                         $is_insert = false;
255                         
256                         //if ( preg_match("/^(insert|delete|update|replace|truncate|drop|create|alter)\s+/i",$query) )
257                         if ( preg_match("/^(insert|delete|update|replace|truncate|drop|create|alter|begin|commit|rollback)/i",$query) )
258                         {
259                                 $this->rows_affected = @$this->dbh->affected_rows;
260
261                                 // Take note of the insert_id
262                                 if ( preg_match("/^(insert|replace)\s+/i",$query) )
263                                 {
264                                         $this->insert_id = @$this->dbh->insert_id;
265                                 }
266
267                                 // Return number fo rows affected
268                                 $return_val = $this->rows_affected;
269                         }
270                         // Query was a select
271                         else
272                         {
273
274                                 // Take note of column info
275                                 $i=0;
276                                 while ($i < @$this->result->field_count)
277                                 {
278                                         $this->col_info[$i] = @$this->result->fetch_field();
279                                         $i++;
280                                 }
281
282                                 // Store Query Results
283                                 $num_rows=0;
284                                 while ( $row = @$this->result->fetch_object() )
285                                 {
286                                         // Store relults as an objects within main array
287                                         $this->last_result[$num_rows] = $row;
288                                         $num_rows++;
289                                 }
290
291                                 @$this->result->free_result();
292
293                                 // Log number of rows the query returned
294                                 $this->num_rows = $num_rows;
295
296                                 // Return number of rows selected
297                                 $return_val = $this->num_rows;
298                         }
299
300                         // disk caching of queries
301                         $this->store_cache($query,$is_insert);
302
303                         // If debug ALL queries
304                         $this->trace || $this->debug_all ? $this->debug() : null ;
305
306                         // Keep tack of how long all queries have taken
307                         $this->timer_update_global($this->num_queries);
308
309                         // Trace all queries
310                         if ( $this->use_trace_log )
311                         {
312                                 $this->trace_log[] = $this->debug(false);
313                         }
314
315                         return $return_val;
316
317                 }
318                 
319                 /**********************************************************************
320                 *  Close the active mySQLi connection
321                 */
322
323                 function disconnect()
324                 {
325                         @$this->dbh->close();
326                 }
327
328         }