3 /**********************************************************************
4 * Author: Justin Vincent (jv@jvmultimedia.com)
5 * Web...: http://twitter.com/justinvincent
7 * Desc..: mySQL component (part of ezSQL databse abstraction library)
11 /**********************************************************************
12 * ezSQL error strings - mySQL
15 $ezsql_mysql_str = array
17 1 => 'Require $dbuser and $dbpassword to connect to a database server',
18 2 => 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?',
19 3 => 'Require $dbname to select a database',
20 4 => 'mySQL database connection is not active',
21 5 => 'Unexpected error while trying to select database'
24 /**********************************************************************
25 * ezSQL Database specific class - mySQL
28 if ( ! function_exists ('mysql_connect') ) die('<b>Fatal Error:</b> ezSQL_mysql requires mySQL 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_mysql extends ezSQLcore
35 var $dbpassword = false;
38 var $encoding = false;
39 var $rows_affected = false;
41 /**********************************************************************
42 * Constructor - allow the user to perform a qucik connect at the
43 * same time as initialising the ezSQL_mysql class
46 function ezSQL_mysql($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $encoding='')
48 $this->dbuser = $dbuser;
49 $this->dbpassword = $dbpassword;
50 $this->dbname = $dbname;
51 $this->dbhost = $dbhost;
52 $this->encoding = $encoding;
55 /**********************************************************************
56 * Short hand way to connect to mySQL database server
57 * and select a mySQL database at the same time
60 function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $encoding='')
63 if ( ! $this->connect($dbuser, $dbpassword, $dbhost,true) ) ;
64 else if ( ! $this->select($dbname,$encoding) ) ;
65 else $return_val = true;
69 /**********************************************************************
70 * Try to connect to mySQL database server
73 function connect($dbuser='', $dbpassword='', $dbhost='localhost')
75 global $ezsql_mysql_str; $return_val = false;
77 // Keep track of how long the DB takes to connect
78 $this->timer_start('db_connect_time');
80 // Must have a user and a password
83 $this->register_error($ezsql_mysql_str[1].' in '.__FILE__.' on line '.__LINE__);
84 $this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
86 // Try to establish the server database handle
87 else if ( ! $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword,true,131074) )
89 $this->register_error($ezsql_mysql_str[2].' in '.__FILE__.' on line '.__LINE__);
90 $this->show_errors ? trigger_error($ezsql_mysql_str[2],E_USER_WARNING) : null;
94 $this->dbuser = $dbuser;
95 $this->dbpassword = $dbpassword;
96 $this->dbhost = $dbhost;
103 /**********************************************************************
104 * Try to select a mySQL database
107 function select($dbname='', $encoding='')
109 global $ezsql_mysql_str; $return_val = false;
111 // Must have a database name
114 $this->register_error($ezsql_mysql_str[3].' in '.__FILE__.' on line '.__LINE__);
115 $this->show_errors ? trigger_error($ezsql_mysql_str[3],E_USER_WARNING) : null;
118 // Must have an active database connection
119 else if ( ! $this->dbh )
121 $this->register_error($ezsql_mysql_str[4].' in '.__FILE__.' on line '.__LINE__);
122 $this->show_errors ? trigger_error($ezsql_mysql_str[4],E_USER_WARNING) : null;
125 // Try to connect to the database
126 else if ( !@mysql_select_db($dbname,$this->dbh) )
128 // Try to get error supplied by mysql if not use our own
129 if ( !$str = @mysql_error($this->dbh))
130 $str = $ezsql_mysql_str[5];
132 $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
133 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
137 $this->dbname = $dbname;
140 $encoding = strtolower(str_replace("-","",$encoding));
142 $result = mysql_query("SHOW CHARACTER SET");
143 while($row = mysql_fetch_array($result,MYSQL_ASSOC))
145 $charsets[] = $row["Charset"];
147 if(in_array($encoding,$charsets)){
148 mysql_query("SET NAMES '".$encoding."'");
158 /**********************************************************************
159 * Format a mySQL string correctly for safe mySQL insert
160 * (no mater if magic quotes are on or not)
163 function escape($str)
165 // If there is no existing database connection then try to connect
166 if ( ! isset($this->dbh) || ! $this->dbh )
168 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
169 $this->select($this->dbname, $this->encoding);
172 return mysql_real_escape_string(stripslashes($str));
175 /**********************************************************************
176 * Return mySQL specific system date syntax
177 * i.e. Oracle: SYSDATE Mysql: NOW()
185 /**********************************************************************
186 * Perform mySQL query and try to detirmin result value
189 function query($query)
192 // This keeps the connection alive for very long running scripts
193 if ( $this->num_queries >= 500 )
195 $this->num_queries = 0;
197 $this->quick_connect($this->dbuser,$this->dbpassword,$this->dbname,$this->dbhost,$this->encoding);
203 // Flush cached values..
206 // For reg expressions
207 $query = trim($query);
209 // Log how the function was called
210 $this->func_call = "\$db->query(\"$query\")";
212 // Keep track of the last query for debug..
213 $this->last_query = $query;
215 // Count how many queries there have been
216 $this->num_queries++;
219 $this->timer_start($this->num_queries);
221 // Use core file cache function
222 if ( $cache = $this->get_cache($query) )
224 // Keep tack of how long all queries have taken
225 $this->timer_update_global($this->num_queries);
228 if ( $this->use_trace_log )
230 $this->trace_log[] = $this->debug(false);
236 // If there is no existing database connection then try to connect
237 if ( ! isset($this->dbh) || ! $this->dbh )
239 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
240 $this->select($this->dbname,$this->encoding);
243 // Perform the query via std mysql_query function..
244 $this->result = @mysql_query($query,$this->dbh);
246 // If there is an error then take note of it..
247 if ( $str = @mysql_error($this->dbh) )
250 $this->register_error($str);
251 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
255 // Query was an insert, delete, update, replace
257 if ( preg_match("/^(insert|delete|update|replace|truncate|drop|create|alter)\s+/i",$query) )
259 $this->rows_affected = @mysql_affected_rows($this->dbh);
261 // Take note of the insert_id
262 if ( preg_match("/^(insert|replace)\s+/i",$query) )
264 $this->insert_id = @mysql_insert_id($this->dbh);
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 < @mysql_num_fields($this->result))
278 $this->col_info[$i] = @mysql_fetch_field($this->result);
282 // Store Query Results
284 while ( $row = @mysql_fetch_object($this->result) )
286 // Store relults as an objects within main array
287 $this->last_result[$num_rows] = $row;
291 @mysql_free_result($this->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 mySQL connection
323 function disconnect()
325 @mysql_close($this->dbh);