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;
40 /**********************************************************************
41 * Constructor - allow the user to perform a qucik connect at the
42 * same time as initialising the ezSQL_mysql class
45 function ezSQL_mysql($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_mysql_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_mysql_str[1].' in '.__FILE__.' on line '.__LINE__);
83 $this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
85 // Try to establish the server database handle
86 else if ( ! $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword,true,131074) )
88 $this->register_error($ezsql_mysql_str[2].' in '.__FILE__.' on line '.__LINE__);
89 $this->show_errors ? trigger_error($ezsql_mysql_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_mysql_str; $return_val = false;
110 // Must have a database name
113 $this->register_error($ezsql_mysql_str[3].' in '.__FILE__.' on line '.__LINE__);
114 $this->show_errors ? trigger_error($ezsql_mysql_str[3],E_USER_WARNING) : null;
117 // Must have an active database connection
118 else if ( ! $this->dbh )
120 $this->register_error($ezsql_mysql_str[4].' in '.__FILE__.' on line '.__LINE__);
121 $this->show_errors ? trigger_error($ezsql_mysql_str[4],E_USER_WARNING) : null;
124 // Try to connect to the database
125 else if ( !@mysql_select_db($dbname,$this->dbh) )
127 // Try to get error supplied by mysql if not use our own
128 if ( !$str = @mysql_error($this->dbh))
129 $str = $ezsql_mysql_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 = mysql_query("SHOW CHARACTER SET");
142 while($row = mysql_fetch_array($result,MYSQL_ASSOC))
144 $charsets[] = $row["Charset"];
146 if(in_array($encoding,$charsets)){
147 mysql_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 mysql_real_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 detirmin 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 = @mysql_query($query,$this->dbh);
244 // If there is an error then take note of it..
245 if ( $str = @mysql_error($this->dbh) )
248 $this->register_error($str);
249 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
253 // Query was an insert, delete, update, replace
255 if ( preg_match("/^(insert|delete|update|replace|truncate|drop|create|alter)\s+/i",$query) )
257 $this->rows_affected = @mysql_affected_rows($this->dbh);
259 // Take note of the insert_id
260 if ( preg_match("/^(insert|replace)\s+/i",$query) )
262 $this->insert_id = @mysql_insert_id($this->dbh);
265 // Return number fo rows affected
266 $return_val = $this->rows_affected;
268 // Query was a select
272 // Take note of column info
274 while ($i < @mysql_num_fields($this->result))
276 $this->col_info[$i] = @mysql_fetch_field($this->result);
280 // Store Query Results
282 while ( $row = @mysql_fetch_object($this->result) )
284 // Store relults as an objects within main array
285 $this->last_result[$num_rows] = $row;
289 @mysql_free_result($this->result);
291 // Log number of rows the query returned
292 $this->num_rows = $num_rows;
294 // Return number of rows selected
295 $return_val = $this->num_rows;
298 // disk caching of queries
299 $this->store_cache($query,$is_insert);
301 // If debug ALL queries
302 $this->trace || $this->debug_all ? $this->debug() : null ;
304 // Keep tack of how long all queries have taken
305 $this->timer_update_global($this->num_queries);
308 if ( $this->use_trace_log )
310 $this->trace_log[] = $this->debug(false);
317 /**********************************************************************
318 * Close the active mySQL connection
321 function disconnect()
323 @mysql_close($this->dbh);