3 /**********************************************************************
4 * Author: Juergen Bouché (jbouche@nurfuerspam.de)
5 * Web...: http://www.juergenbouche.de
7 * Desc..: mySQLi component (part of ezSQL databse abstraction library)
11 /**********************************************************************
12 * ezSQL error strings - mySQLi
15 $ezsql_mysqli_str = array
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'
24 /**********************************************************************
25 * ezSQL Database specific class - mySQLi
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');
31 class ezSQL_mysqli extends ezSQLcore
35 var $dbpassword = false;
38 var $encoding = false;
40 /**********************************************************************
41 * Constructor - allow the user to perform a qucik connect at the
42 * same time as initialising the ezSQL_mysqli class
45 function ezSQL_mysqli($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $encoding='')
47 $this->dbuser = $dbuser;
48 $this->dbpassword = $dbpassword;
49 $this->dbname = $dbname;
50 $this->dbhost = $dbhost;
51 $this->encoding = $encoding;
54 /**********************************************************************
55 * Short hand way to connect to mySQL database server
56 * and select a mySQL database at the same time
59 function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $encoding='')
62 if ( ! $this->connect($dbuser, $dbpassword, $dbhost,true) ) ;
63 else if ( ! $this->select($dbname,$encoding) ) ;
64 else $return_val = true;
68 /**********************************************************************
69 * Try to connect to mySQL database server
72 function connect($dbuser='', $dbpassword='', $dbhost='localhost')
74 global $ezsql_mysqli_str; $return_val = false;
76 // Keep track of how long the DB takes to connect
77 $this->timer_start('db_connect_time');
79 // Must have a user and a password
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;
85 // Try to establish the server database handle
86 else if ( ! $this->dbh = new mysqli($dbhost,$dbuser,$dbpassword) )
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;
93 $this->dbuser = $dbuser;
94 $this->dbpassword = $dbpassword;
95 $this->dbhost = $dbhost;
102 /**********************************************************************
103 * Try to select a mySQL database
106 function select($dbname='', $encoding='')
108 global $ezsql_mysqli_str; $return_val = false;
110 // Must have a database name
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;
117 // Must have an active database connection
118 else if ( ! $this->dbh )
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;
124 // Try to connect to the database
125 else if ( !@$this->dbh->select_db($dbname) )
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];
131 $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
132 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
136 $this->dbname = $dbname;
139 $encoding = strtolower(str_replace("-","",$encoding));
141 $result = $this->dbh->query("SHOW CHARACTER SET");
142 while($row = $result->fetch_array(MYSQLI_ASSOC))
144 $charsets[] = $row["Charset"];
146 if(in_array($encoding,$charsets)){
147 $this->dbh->query("SET NAMES '".$encoding."'");
157 /**********************************************************************
158 * Format a mySQL string correctly for safe mySQL insert
159 * (no mater if magic quotes are on or not)
162 function escape($str)
164 // If there is no existing database connection then try to connect
165 if ( ! isset($this->dbh) || ! $this->dbh )
167 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
168 $this->select($this->dbname, $this->encoding);
171 return $this->dbh->escape_string(stripslashes($str));
174 /**********************************************************************
175 * Return mySQL specific system date syntax
176 * i.e. Oracle: SYSDATE Mysql: NOW()
184 /**********************************************************************
185 * Perform mySQL query and try to determine result value
188 function query($query)
191 // This keeps the connection alive for very long running scripts
192 if ( $this->num_queries >= 500 )
195 $this->quick_connect($this->dbuser,$this->dbpassword,$this->dbname,$this->dbhost,$this->encoding);
201 // Flush cached values..
204 // For reg expressions
205 $query = trim($query);
207 // Log how the function was called
208 $this->func_call = "\$db->query(\"$query\")";
210 // Keep track of the last query for debug..
211 $this->last_query = $query;
213 // Count how many queries there have been
214 $this->num_queries++;
217 $this->timer_start($this->num_queries);
219 // Use core file cache function
220 if ( $cache = $this->get_cache($query) )
222 // Keep tack of how long all queries have taken
223 $this->timer_update_global($this->num_queries);
226 if ( $this->use_trace_log )
228 $this->trace_log[] = $this->debug(false);
234 // If there is no existing database connection then try to connect
235 if ( ! isset($this->dbh) || ! $this->dbh )
237 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
238 $this->select($this->dbname,$this->encoding);
241 // Perform the query via std mysql_query function..
242 $this->result = @$this->dbh->query($query);
244 // If there is an error then take note of it..
245 if ( $str = @$this->dbh->error )
248 $this->register_error($str);
249 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
253 // Query was an insert, delete, update, replace
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) )
259 $this->rows_affected = @$this->dbh->affected_rows;
261 // Take note of the insert_id
262 if ( preg_match("/^(insert|replace)\s+/i",$query) )
264 $this->insert_id = @$this->dbh->insert_id;
267 // Return number fo rows affected
268 $return_val = $this->rows_affected;
270 // Query was a select
274 // Take note of column info
276 while ($i < @$this->result->field_count)
278 $this->col_info[$i] = @$this->result->fetch_field();
282 // Store Query Results
284 while ( $row = @$this->result->fetch_object() )
286 // Store relults as an objects within main array
287 $this->last_result[$num_rows] = $row;
291 @$this->result->free_result();
293 // Log number of rows the query returned
294 $this->num_rows = $num_rows;
296 // Return number of rows selected
297 $return_val = $this->num_rows;
300 // disk caching of queries
301 $this->store_cache($query,$is_insert);
303 // If debug ALL queries
304 $this->trace || $this->debug_all ? $this->debug() : null ;
306 // Keep tack of how long all queries have taken
307 $this->timer_update_global($this->num_queries);
310 if ( $this->use_trace_log )
312 $this->trace_log[] = $this->debug(false);
319 /**********************************************************************
320 * Close the active mySQLi connection
323 function disconnect()
325 @$this->dbh->close();