]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/ezSQL/ez_sql_mysql.php
Add mysql, mysqli and PDO support
[Github/YOURLS.git] / includes / ezSQL / ez_sql_mysql.php
1 <?php
2
3         /**********************************************************************
4         *  Author: Justin Vincent (jv@jvmultimedia.com)
5         *  Web...: http://twitter.com/justinvincent
6         *  Name..: ezSQL_mysql
7         *  Desc..: mySQL component (part of ezSQL databse abstraction library)
8         *
9         */
10
11         /**********************************************************************
12         *  ezSQL error strings - mySQL
13         */
14
15         $ezsql_mysql_str = array
16         (
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'
22         );
23
24         /**********************************************************************
25         *  ezSQL Database specific class - mySQL
26         */
27
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');
30
31         class ezSQL_mysql 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_mysql class
43                 */
44
45                 function ezSQL_mysql($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_mysql_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_mysql_str[1].' in '.__FILE__.' on line '.__LINE__);
83                                 $this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
84                         }
85                         // Try to establish the server database handle
86                         else if ( ! $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword,true,131074) )
87                         {
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;
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_mysql_str; $return_val = false;
109
110                         // Must have a database name
111                         if ( ! $dbname )
112                         {
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;
115                         }
116
117                         // Must have an active database connection
118                         else if ( ! $this->dbh )
119                         {
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;
122                         }
123
124                         // Try to connect to the database
125                         else if ( !@mysql_select_db($dbname,$this->dbh) )
126                         {
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];
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 = mysql_query("SHOW CHARACTER SET");
142                                         while($row = mysql_fetch_array($result,MYSQL_ASSOC))
143                                         {
144                                                 $charsets[] = $row["Charset"];
145                                         }
146                                         if(in_array($encoding,$charsets)){
147                                                 mysql_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 mysql_real_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 detirmin 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 = @mysql_query($query,$this->dbh);
243
244                         // If there is an error then take note of it..
245                         if ( $str = @mysql_error($this->dbh) )
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                         if ( preg_match("/^(insert|delete|update|replace|truncate|drop|create|alter)\s+/i",$query) )
256                         {
257                                 $this->rows_affected = @mysql_affected_rows($this->dbh);
258
259                                 // Take note of the insert_id
260                                 if ( preg_match("/^(insert|replace)\s+/i",$query) )
261                                 {
262                                         $this->insert_id = @mysql_insert_id($this->dbh);
263                                 }
264
265                                 // Return number fo rows affected
266                                 $return_val = $this->rows_affected;
267                         }
268                         // Query was a select
269                         else
270                         {
271
272                                 // Take note of column info
273                                 $i=0;
274                                 while ($i < @mysql_num_fields($this->result))
275                                 {
276                                         $this->col_info[$i] = @mysql_fetch_field($this->result);
277                                         $i++;
278                                 }
279
280                                 // Store Query Results
281                                 $num_rows=0;
282                                 while ( $row = @mysql_fetch_object($this->result) )
283                                 {
284                                         // Store relults as an objects within main array
285                                         $this->last_result[$num_rows] = $row;
286                                         $num_rows++;
287                                 }
288
289                                 @mysql_free_result($this->result);
290
291                                 // Log number of rows the query returned
292                                 $this->num_rows = $num_rows;
293
294                                 // Return number of rows selected
295                                 $return_val = $this->num_rows;
296                         }
297
298                         // disk caching of queries
299                         $this->store_cache($query,$is_insert);
300
301                         // If debug ALL queries
302                         $this->trace || $this->debug_all ? $this->debug() : null ;
303
304                         // Keep tack of how long all queries have taken
305                         $this->timer_update_global($this->num_queries);
306
307                         // Trace all queries
308                         if ( $this->use_trace_log )
309                         {
310                                 $this->trace_log[] = $this->debug(false);
311                         }
312
313                         return $return_val;
314
315                 }
316                 
317                 /**********************************************************************
318                 *  Close the active mySQL connection
319                 */
320
321                 function disconnect()
322                 {
323                         @mysql_close($this->dbh);       
324                 }
325
326         }