3 /**********************************************************************
4 * Author: Juergen Bouché (jbouche@nurfuerspam.de)
5 * Web...: http://www.juergenbouche.de
7 * Desc..: mySQLi component (part of ezSQL database abstraction library)
11 /**********************************************************************
12 * ezSQL error strings - mySQLi
15 global $ezsql_mysqli_str;
17 $ezsql_mysqli_str = array
19 1 => 'Require $dbuser and $dbpassword to connect to a database server',
20 2 => 'Error establishing mySQLi database connection. Correct user/password? Correct hostname? Database server running?',
21 3 => 'Require $dbname to select a database',
22 4 => 'mySQLi database connection is not active',
23 5 => 'Unexpected error while trying to select database'
26 /**********************************************************************
27 * ezSQL Database specific class - mySQLi
30 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');
31 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');
33 class ezSQL_mysqli extends ezSQLcore
37 var $dbpassword = false;
41 var $encoding = false;
42 var $rows_affected = false;
44 /**********************************************************************
45 * Constructor - allow the user to perform a qucik connect at the
46 * same time as initialising the ezSQL_mysqli class
49 function ezSQL_mysqli($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $encoding='')
51 $this->dbuser = $dbuser;
52 $this->dbpassword = $dbpassword;
53 $this->dbname = $dbname;
54 list( $this->dbhost, $this->dbport ) = $this->get_host_port( $dbhost, 3306 );
55 $this->encoding = $encoding;
58 /**********************************************************************
59 * Short hand way to connect to mySQL database server
60 * and select a mySQL database at the same time
63 function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $dbport='3306', $encoding='')
66 if ( ! $this->connect($dbuser, $dbpassword, $dbhost, $dbport) ) ;
67 else if ( ! $this->select($dbname,$encoding) ) ;
68 else $return_val = true;
72 /**********************************************************************
73 * Try to connect to mySQL database server
76 function connect($dbuser='', $dbpassword='', $dbhost='localhost', $dbport=false)
78 global $ezsql_mysqli_str; $return_val = false;
80 // Keep track of how long the DB takes to connect
81 $this->timer_start('db_connect_time');
83 // If port not specified (new connection issued), get it
85 list( $dbhost, $dbport ) = $this->get_host_port( $dbhost, 3306 );
88 // Must have a user and a password
91 $this->register_error($ezsql_mysqli_str[1].' in '.__FILE__.' on line '.__LINE__);
92 $this->show_errors ? trigger_error($ezsql_mysqli_str[1],E_USER_WARNING) : null;
94 // Try to establish the server database handle
97 $this->dbh = new mysqli($dbhost,$dbuser,$dbpassword, '', $dbport);
98 // Check for connection problem
99 if( $this->dbh->connect_errno )
101 $this->register_error($ezsql_mysqli_str[2].' in '.__FILE__.' on line '.__LINE__);
102 $this->show_errors ? trigger_error($ezsql_mysqli_str[2],E_USER_WARNING) : null;
106 $this->dbuser = $dbuser;
107 $this->dbpassword = $dbpassword;
108 $this->dbhost = $dbhost;
109 $this->dbport = $dbport;
117 /**********************************************************************
118 * Try to select a mySQL database
121 function select($dbname='', $encoding='')
123 global $ezsql_mysqli_str; $return_val = false;
125 // Must have a database name
128 $this->register_error($ezsql_mysqli_str[3].' in '.__FILE__.' on line '.__LINE__);
129 $this->show_errors ? trigger_error($ezsql_mysqli_str[3],E_USER_WARNING) : null;
132 // Must have an active database connection
133 else if ( ! $this->dbh )
135 $this->register_error($ezsql_mysqli_str[4].' in '.__FILE__.' on line '.__LINE__);
136 $this->show_errors ? trigger_error($ezsql_mysqli_str[4],E_USER_WARNING) : null;
139 // Try to connect to the database
140 else if ( !@$this->dbh->select_db($dbname) )
142 // Try to get error supplied by mysql if not use our own
143 if ( !$str = @$this->dbh->error)
144 $str = $ezsql_mysqli_str[5];
146 $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
147 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
151 $this->dbname = $dbname;
154 $encoding = strtolower(str_replace("-","",$encoding));
156 $result = $this->dbh->query("SHOW CHARACTER SET");
157 while($row = $result->fetch_array(MYSQLI_ASSOC))
159 $charsets[] = $row["Charset"];
161 if(in_array($encoding,$charsets)){
162 $this->dbh->query("SET NAMES '".$encoding."'");
172 /**********************************************************************
173 * Format a mySQL string correctly for safe mySQL insert
174 * (no mater if magic quotes are on or not)
177 function escape($str)
179 // If there is no existing database connection then try to connect
180 if ( ! isset($this->dbh) || ! $this->dbh )
182 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost, $this->dbport);
183 $this->select($this->dbname, $this->encoding);
186 return $this->dbh->escape_string(stripslashes($str));
189 /**********************************************************************
190 * Return mySQL specific system date syntax
191 * i.e. Oracle: SYSDATE Mysql: NOW()
199 /**********************************************************************
200 * Perform mySQL query and try to determine result value
203 function query($query)
206 // This keeps the connection alive for very long running scripts
207 if ( $this->num_queries >= 500 )
210 $this->quick_connect($this->dbuser,$this->dbpassword,$this->dbname,$this->dbhost,$this->dbport,$this->encoding);
216 // Flush cached values..
219 // For reg expressions
220 $query = trim($query);
222 // Log how the function was called
223 $this->func_call = "\$db->query(\"$query\")";
225 // Keep track of the last query for debug..
226 $this->last_query = $query;
228 // Count how many queries there have been
229 $this->num_queries++;
232 $this->timer_start($this->num_queries);
234 // Use core file cache function
235 if ( $cache = $this->get_cache($query) )
237 // Keep tack of how long all queries have taken
238 $this->timer_update_global($this->num_queries);
241 if ( $this->use_trace_log )
243 $this->trace_log[] = $this->debug(false);
249 // If there is no existing database connection then try to connect
250 if ( ! isset($this->dbh) || ! $this->dbh )
252 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost, $this->dbport);
253 $this->select($this->dbname,$this->encoding);
256 // Perform the query via std mysql_query function..
257 $this->result = @$this->dbh->query($query);
259 // If there is an error then take note of it..
260 if ( $str = @$this->dbh->error )
263 $this->register_error($str);
264 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
268 // Query was an insert, delete, update, replace
271 //if ( preg_match("/^(insert|delete|update|replace|truncate|drop|create|alter)\s+/i",$query) )
272 if ( preg_match("/^(insert|delete|update|replace|truncate|drop|create|alter|begin|commit|rollback|set)/i",$query) )
274 $this->rows_affected = @$this->dbh->affected_rows;
276 // Take note of the insert_id
277 if ( preg_match("/^(insert|replace)\s+/i",$query) )
279 $this->insert_id = @$this->dbh->insert_id;
282 // Return number fo rows affected
283 $return_val = $this->rows_affected;
285 // Query was a select
289 // Take note of column info
291 while ($i < @$this->result->field_count)
293 $this->col_info[$i] = @$this->result->fetch_field();
297 // Store Query Results
299 while ( $row = @$this->result->fetch_object() )
301 // Store relults as an objects within main array
302 $this->last_result[$num_rows] = $row;
306 @$this->result->free_result();
308 // Log number of rows the query returned
309 $this->num_rows = $num_rows;
311 // Return number of rows selected
312 $return_val = $this->num_rows;
315 // disk caching of queries
316 $this->store_cache($query,$is_insert);
318 // If debug ALL queries
319 $this->trace || $this->debug_all ? $this->debug() : null ;
321 // Keep tack of how long all queries have taken
322 $this->timer_update_global($this->num_queries);
325 if ( $this->use_trace_log )
327 $this->trace_log[] = $this->debug(false);
334 /**********************************************************************
335 * Close the active mySQLi connection
338 function disconnect()
340 @$this->dbh->close();