]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/wp-db.php
Wordpress 3.0-scripts
[autoinstalls/wordpress.git] / wp-includes / wp-db.php
1 <?php
2 /**
3  * WordPress DB Class
4  *
5  * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
6  *
7  * @package WordPress
8  * @subpackage Database
9  * @since 0.71
10  */
11
12 /**
13  * @since 0.71
14  */
15 define( 'EZSQL_VERSION', 'WP1.25' );
16
17 /**
18  * @since 0.71
19  */
20 define( 'OBJECT', 'OBJECT', true );
21
22 /**
23  * @since 2.5.0
24  */
25 define( 'OBJECT_K', 'OBJECT_K' );
26
27 /**
28  * @since 0.71
29  */
30 define( 'ARRAY_A', 'ARRAY_A' );
31
32 /**
33  * @since 0.71
34  */
35 define( 'ARRAY_N', 'ARRAY_N' );
36
37 /**
38  * WordPress Database Access Abstraction Object
39  *
40  * It is possible to replace this class with your own
41  * by setting the $wpdb global variable in wp-content/db.php
42  * file with your class. You can name it wpdb also, since
43  * this file will not be included, if the other file is
44  * available.
45  *
46  * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
47  *
48  * @package WordPress
49  * @subpackage Database
50  * @since 0.71
51  * @final
52  */
53 class wpdb {
54
55         /**
56          * Whether to show SQL/DB errors
57          *
58          * @since 0.71
59          * @access private
60          * @var bool
61          */
62         var $show_errors = false;
63
64         /**
65          * Whether to suppress errors during the DB bootstrapping.
66          *
67          * @access private
68          * @since 2.5
69          * @var bool
70          */
71         var $suppress_errors = false;
72
73         /**
74          * The last error during query.
75          *
76          * @see get_last_error()
77          * @since 2.5
78          * @access private
79          * @var string
80          */
81         var $last_error = '';
82
83         /**
84          * Amount of queries made
85          *
86          * @since 1.2.0
87          * @access private
88          * @var int
89          */
90         var $num_queries = 0;
91
92         /**
93          * Count of rows returned by previous query
94          *
95          * @since 1.2
96          * @access private
97          * @var int
98          */
99         var $num_rows = 0;
100
101         /**
102          * Count of affected rows by previous query
103          *
104          * @since 0.71
105          * @access private
106          * @var int
107          */
108         var $rows_affected = 0;
109
110         /**
111          * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
112          *
113          * @since 0.71
114          * @access public
115          * @var int
116          */
117         var $insert_id = 0;
118
119         /**
120          * Saved result of the last query made
121          *
122          * @since 1.2.0
123          * @access private
124          * @var array
125          */
126         var $last_query;
127
128         /**
129          * Results of the last query made
130          *
131          * @since 1.0.0
132          * @access private
133          * @var array|null
134          */
135         var $last_result;
136
137         /**
138          * Saved info on the table column
139          *
140          * @since 1.2.0
141          * @access private
142          * @var array
143          */
144         var $col_info;
145
146         /**
147          * Saved queries that were executed
148          *
149          * @since 1.5.0
150          * @access private
151          * @var array
152          */
153         var $queries;
154
155         /**
156          * WordPress table prefix
157          *
158          * You can set this to have multiple WordPress installations
159          * in a single database. The second reason is for possible
160          * security precautions.
161          *
162          * @since 0.71
163          * @access private
164          * @var string
165          */
166         var $prefix = '';
167
168         /**
169          * Whether the database queries are ready to start executing.
170          *
171          * @since 2.5.0
172          * @access private
173          * @var bool
174          */
175         var $ready = false;
176
177         /**
178          * {@internal Missing Description}}
179          *
180          * @since 3.0.0
181          * @access public
182          * @var int
183          */
184         var $blogid = 0;
185
186         /**
187          * {@internal Missing Description}}
188          *
189          * @since 3.0.0
190          * @access public
191          * @var int
192          */
193         var $siteid = 0;
194
195         /**
196          * List of WordPress per-blog tables
197          *
198          * @since 2.5.0
199          * @access private
200          * @see wpdb::tables()
201          * @var array
202          */
203         var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
204                 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
205
206         /**
207          * List of deprecated WordPress tables
208          *
209          * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
210          *
211          * @since 2.9.0
212          * @access private
213          * @see wpdb::tables()
214          * @var array
215          */
216         var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
217
218         /**
219          * List of WordPress global tables
220          *
221          * @since 3.0.0
222          * @access private
223          * @see wpdb::tables()
224          * @var array
225          */
226         var $global_tables = array( 'users', 'usermeta' );
227
228         /**
229          * List of Multisite global tables
230          *
231          * @since 3.0.0
232          * @access private
233          * @see wpdb::tables()
234          * @var array
235          */
236         var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
237                 'sitecategories', 'registration_log', 'blog_versions' );
238
239         /**
240          * WordPress Comments table
241          *
242          * @since 1.5.0
243          * @access public
244          * @var string
245          */
246         var $comments;
247
248         /**
249          * WordPress Comment Metadata table
250          *
251          * @since 2.9.0
252          * @access public
253          * @var string
254          */
255         var $commentmeta;
256
257         /**
258          * WordPress Links table
259          *
260          * @since 1.5.0
261          * @access public
262          * @var string
263          */
264         var $links;
265
266         /**
267          * WordPress Options table
268          *
269          * @since 1.5.0
270          * @access public
271          * @var string
272          */
273         var $options;
274
275         /**
276          * WordPress Post Metadata table
277          *
278          * @since 1.5.0
279          * @access public
280          * @var string
281          */
282         var $postmeta;
283
284         /**
285          * WordPress Posts table
286          *
287          * @since 1.5.0
288          * @access public
289          * @var string
290          */
291         var $posts;
292
293         /**
294          * WordPress Terms table
295          *
296          * @since 2.3.0
297          * @access public
298          * @var string
299          */
300         var $terms;
301
302         /**
303          * WordPress Term Relationships table
304          *
305          * @since 2.3.0
306          * @access public
307          * @var string
308          */
309         var $term_relationships;
310
311         /**
312          * WordPress Term Taxonomy table
313          *
314          * @since 2.3.0
315          * @access public
316          * @var string
317          */
318         var $term_taxonomy;
319
320         /*
321          * Global and Multisite tables
322          */
323
324         /**
325          * WordPress User Metadata table
326          *
327          * @since 2.3.0
328          * @access public
329          * @var string
330          */
331         var $usermeta;
332
333         /**
334          * WordPress Users table
335          *
336          * @since 1.5.0
337          * @access public
338          * @var string
339          */
340         var $users;
341
342         /**
343          * Multisite Blogs table
344          *
345          * @since 3.0.0
346          * @access public
347          * @var string
348          */
349         var $blogs;
350
351         /**
352          * Multisite Blog Versions table
353          *
354          * @since 3.0.0
355          * @access public
356          * @var string
357          */
358         var $blog_versions;
359
360         /**
361          * Multisite Registration Log table
362          *
363          * @since 3.0.0
364          * @access public
365          * @var string
366          */
367         var $registration_log;
368
369         /**
370          * Multisite Signups table
371          *
372          * @since 3.0.0
373          * @access public
374          * @var string
375          */
376         var $signups;
377
378         /**
379          * Multisite Sites table
380          *
381          * @since 3.0.0
382          * @access public
383          * @var string
384          */
385         var $site;
386
387         /**
388          * Multisite Sitewide Terms table
389          *
390          * @since 3.0.0
391          * @access public
392          * @var string
393          */
394         var $sitecategories;
395
396         /**
397          * Multisite Site Metadata table
398          *
399          * @since 3.0.0
400          * @access public
401          * @var string
402          */
403         var $sitemeta;
404
405         /**
406          * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
407          *
408          * Keys are column names, values are format types: 'ID' => '%d'
409          *
410          * @since 2.8.0
411          * @see wpdb:prepare()
412          * @see wpdb:insert()
413          * @see wpdb:update()
414          * @see wp_set_wpdb_vars()
415          * @access public
416          * @var array
417          */
418         var $field_types = array();
419
420         /**
421          * Database table columns charset
422          *
423          * @since 2.2.0
424          * @access public
425          * @var string
426          */
427         var $charset;
428
429         /**
430          * Database table columns collate
431          *
432          * @since 2.2.0
433          * @access public
434          * @var string
435          */
436         var $collate;
437
438         /**
439          * Whether to use mysql_real_escape_string
440          *
441          * @since 2.8.0
442          * @access public
443          * @var bool
444          */
445         var $real_escape = false;
446
447         /**
448          * Database Username
449          *
450          * @since 2.9.0
451          * @access private
452          * @var string
453          */
454         var $dbuser;
455
456         /**
457          * A textual description of the last query/get_row/get_var call
458          *
459          * @since unknown
460          * @access public
461          * @var string
462          */
463         var $func_call;
464
465         /**
466          * Connects to the database server and selects a database
467          *
468          * PHP4 compatibility layer for calling the PHP5 constructor.
469          *
470          * @uses wpdb::__construct() Passes parameters and returns result
471          * @since 0.71
472          *
473          * @param string $dbuser MySQL database user
474          * @param string $dbpassword MySQL database password
475          * @param string $dbname MySQL database name
476          * @param string $dbhost MySQL database host
477          */
478         function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
479                 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )
480                         $this->db_connect();
481                 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
482         }
483
484         /**
485          * Connects to the database server and selects a database
486          *
487          * PHP5 style constructor for compatibility with PHP5. Does
488          * the actual setting up of the class properties and connection
489          * to the database.
490          *
491          * @link http://core.trac.wordpress.org/ticket/3354
492          * @since 2.0.8
493          *
494          * @param string $dbuser MySQL database user
495          * @param string $dbpassword MySQL database password
496          * @param string $dbname MySQL database name
497          * @param string $dbhost MySQL database host
498          */
499         function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
500                 register_shutdown_function( array( &$this, '__destruct' ) );
501
502                 if ( WP_DEBUG )
503                         $this->show_errors();
504
505                 if ( is_multisite() ) {
506                         $this->charset = 'utf8';
507                         if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
508                                 $this->collate = DB_COLLATE;
509                         else
510                                 $this->collate = 'utf8_general_ci';
511                 } elseif ( defined( 'DB_COLLATE' ) ) {
512                         $this->collate = DB_COLLATE;
513                 }
514
515                 if ( defined( 'DB_CHARSET' ) )
516                         $this->charset = DB_CHARSET;
517
518                 $this->dbuser = $dbuser;
519
520                 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true );
521                 if ( !$this->dbh ) {
522                         $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
523 <h1>Error establishing a database connection</h1>
524 <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p>
525 <ul>
526         <li>Are you sure you have the correct username and password?</li>
527         <li>Are you sure that you have typed the correct hostname?</li>
528         <li>Are you sure that the database server is running?</li>
529 </ul>
530 <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
531 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' );
532                         return;
533                 }
534
535                 $this->ready = true;
536
537                 if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) {
538                         if ( function_exists( 'mysql_set_charset' ) ) {
539                                 mysql_set_charset( $this->charset, $this->dbh );
540                                 $this->real_escape = true;
541                         } else {
542                                 $query = $this->prepare( 'SET NAMES %s', $this->charset );
543                                 if ( ! empty( $this->collate ) )
544                                         $query .= $this->prepare( ' COLLATE %s', $this->collate );
545                                 $this->query( $query );
546                         }
547                 }
548
549                 $this->select( $dbname, $this->dbh );
550         }
551
552         /**
553          * PHP5 style destructor and will run when database object is destroyed.
554          *
555          * @see wpdb::__construct()
556          * @since 2.0.8
557          * @return bool true
558          */
559         function __destruct() {
560                 return true;
561         }
562
563         /**
564          * Sets the table prefix for the WordPress tables.
565          *
566          * @since 2.5.0
567          *
568          * @param string $prefix Alphanumeric name for the new prefix.
569          * @return string|WP_Error Old prefix or WP_Error on error
570          */
571         function set_prefix( $prefix, $set_table_names = true ) {
572
573                 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
574                         return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
575
576                 $old_prefix = is_multisite() ? '' : $prefix;
577
578                 if ( isset( $this->base_prefix ) )
579                         $old_prefix = $this->base_prefix;
580
581                 $this->base_prefix = $prefix;
582
583                 if ( $set_table_names ) {
584                         foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
585                                 $this->$table = $prefixed_table;
586
587                         if ( is_multisite() && empty( $this->blogid ) )
588                                 return $old_prefix;
589
590                         $this->prefix = $this->get_blog_prefix();
591
592                         foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
593                                 $this->$table = $prefixed_table;
594
595                         foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
596                                 $this->$table = $prefixed_table;
597                 }
598                 return $old_prefix;
599         }
600
601         /**
602          * Sets blog id.
603          *
604          * @since 3.0.0
605          * @access public
606          * @param int $blog_id
607          * @param int $site_id Optional.
608          * @return string previous blog id
609          */
610         function set_blog_id( $blog_id, $site_id = 0 ) {
611                 if ( ! empty( $site_id ) )
612                         $this->siteid = $site_id;
613
614                 $old_blog_id  = $this->blogid;
615                 $this->blogid = $blog_id;
616
617                 $this->prefix = $this->get_blog_prefix();
618
619                 foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
620                         $this->$table = $prefixed_table;
621
622                 foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
623                         $this->$table = $prefixed_table;
624
625                 return $old_blog_id;
626         }
627
628         /**
629          * Gets blog prefix.
630          *
631          * @uses is_multisite()
632          * @since 3.0.0
633          * @param int $blog_id Optional.
634          * @return string Blog prefix.
635          */
636         function get_blog_prefix( $blog_id = null ) {
637                 if ( is_multisite() ) {
638                         if ( null === $blog_id )
639                                 $blog_id = $this->blogid;
640                         if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )
641                                 return $this->base_prefix;
642                         else
643                                 return $this->base_prefix . $blog_id . '_';
644                 } else {
645                         return $this->base_prefix;
646                 }
647         }
648
649         /**
650          * Returns an array of WordPress tables.
651          *
652          * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
653          * override the WordPress users and usersmeta tables that would otherwise
654          * be determined by the prefix.
655          *
656          * The scope argument can take one of the following:
657          *
658          * 'all' - returns 'all' and 'global' tables. No old tables are returned.
659          * 'blog' - returns the blog-level tables for the queried blog.
660          * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite.
661          * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite.
662          * 'old' - returns tables which are deprecated.
663          *
664          * @since 3.0.0
665          * @uses wpdb::$tables
666          * @uses wpdb::$old_tables
667          * @uses wpdb::$global_tables
668          * @uses wpdb::$ms_global_tables
669          * @uses is_multisite()
670          *
671          * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
672          * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
673          *      prefix is requested, then the custom users and usermeta tables will be mapped.
674          * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested.
675          * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
676          */
677         function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
678                 switch ( $scope ) {
679                         case 'all' :
680                                 $tables = array_merge( $this->global_tables, $this->tables );
681                                 if ( is_multisite() )
682                                         $tables = array_merge( $tables, $this->ms_global_tables );
683                                 break;
684                         case 'blog' :
685                                 $tables = $this->tables;
686                                 break;
687                         case 'global' :
688                                 $tables = $this->global_tables;
689                                 if ( is_multisite() )
690                                         $tables = array_merge( $tables, $this->ms_global_tables );
691                                 break;
692                         case 'ms_global' :
693                                 $tables = $this->ms_global_tables;
694                                 break;
695                         case 'old' :
696                                 $tables = $this->old_tables;
697                                 break;
698                         default :
699                                 return array();
700                                 break;
701                 }
702
703                 if ( $prefix ) {
704                         if ( ! $blog_id )
705                                 $blog_id = $this->blogid;
706                         $blog_prefix = $this->get_blog_prefix( $blog_id );
707                         $base_prefix = $this->base_prefix;
708                         $global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
709                         foreach ( $tables as $k => $table ) {
710                                 if ( in_array( $table, $global_tables ) )
711                                         $tables[ $table ] = $base_prefix . $table;
712                                 else
713                                         $tables[ $table ] = $blog_prefix . $table;
714                                 unset( $tables[ $k ] );
715                         }
716
717                         if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
718                                 $tables['users'] = CUSTOM_USER_TABLE;
719
720                         if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
721                                 $tables['usermeta'] = CUSTOM_USER_META_TABLE;
722                 }
723
724                 return $tables;
725         }
726
727         /**
728          * Selects a database using the current database connection.
729          *
730          * The database name will be changed based on the current database
731          * connection. On failure, the execution will bail and display an DB error.
732          *
733          * @since 0.71
734          *
735          * @param string $db MySQL database name
736          * @param resource $dbh Optional link identifier.
737          * @return null Always null.
738          */
739         function select( $db, $dbh = null) {
740                 if ( is_null($dbh) ) 
741                         $dbh = $this->dbh;
742
743                 if ( !@mysql_select_db( $db, $dbh ) ) {
744                         $this->ready = false;
745                         $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'
746 <h1>Can&#8217;t select database</h1>
747 <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p>
748 <ul>
749 <li>Are you sure it exists?</li>
750 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
751 <li>On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?</li>
752 </ul>
753 <p>If you don\'t know how to set up a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>'/*/WP_I18N_DB_SELECT_DB*/, $db, $this->dbuser ), 'db_select_fail' );
754                         return;
755                 }
756         }
757
758         /**
759          * Weak escape, using addslashes()
760          *
761          * @see addslashes()
762          * @since 2.8.0
763          * @access private
764          *
765          * @param string $string
766          * @return string
767          */
768         function _weak_escape( $string ) {
769                 return addslashes( $string );
770         }
771
772         /**
773          * Real escape, using mysql_real_escape_string() or addslashes()
774          *
775          * @see mysql_real_escape_string()
776          * @see addslashes()
777          * @since 2.8
778          * @access private
779          *
780          * @param  string $string to escape
781          * @return string escaped
782          */
783         function _real_escape( $string ) {
784                 if ( $this->dbh && $this->real_escape )
785                         return mysql_real_escape_string( $string, $this->dbh );
786                 else
787                         return addslashes( $string );
788         }
789
790         /**
791          * Escape data. Works on arrays.
792          *
793      * @uses wpdb::_escape()
794      * @uses wpdb::_real_escape()
795          * @since  2.8
796          * @access private
797          *
798          * @param  string|array $data
799          * @return string|array escaped
800          */
801         function _escape( $data ) {
802                 if ( is_array( $data ) ) {
803                         foreach ( (array) $data as $k => $v ) {
804                                 if ( is_array($v) )
805                                         $data[$k] = $this->_escape( $v );
806                                 else
807                                         $data[$k] = $this->_real_escape( $v );
808                         }
809                 } else {
810                         $data = $this->_real_escape( $data );
811                 }
812
813                 return $data;
814         }
815
816         /**
817          * Escapes content for insertion into the database using addslashes(), for security.
818          *
819          * Works on arrays.
820          *
821          * @since 0.71
822          * @param string|array $data to escape
823          * @return string|array escaped as query safe string
824          */
825         function escape( $data ) {
826                 if ( is_array( $data ) ) {
827                         foreach ( (array) $data as $k => $v ) {
828                                 if ( is_array( $v ) )
829                                         $data[$k] = $this->escape( $v );
830                                 else
831                                         $data[$k] = $this->_weak_escape( $v );
832                         }
833                 } else {
834                         $data = $this->_weak_escape( $data );
835                 }
836
837                 return $data;
838         }
839
840         /**
841          * Escapes content by reference for insertion into the database, for security
842          *
843          * @uses wpdb::_real_escape()
844          * @since 2.3.0
845          * @param string $string to escape
846          * @return void
847          */
848         function escape_by_ref( &$string ) {
849                 $string = $this->_real_escape( $string );
850         }
851
852         /**
853          * Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
854          *
855          * The following directives can be used in the query format string:
856          *   %d (decimal number)
857          *   %s (string)
858          *   %% (literal percentage sign - no argument needed)
859          *
860          * Both %d and %s are to be left unquoted in the query string and they need an argument passed for them.
861          * Literals (%) as parts of the query must be properly written as %%.
862          *
863          * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string).
864          * Does not support sign, padding, alignment, width or precision specifiers.
865          * Does not support argument numbering/swapping.
866          *
867          * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}.
868          *
869          * Both %d and %s should be left unquoted in the query string.
870          *
871          * <code>
872          * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
873          * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
874          * </code>
875          *
876          * @link http://php.net/sprintf Description of syntax.
877          * @since 2.3.0
878          *
879          * @param string $query Query statement with sprintf()-like placeholders
880          * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like
881          *      {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if
882          *      being called like {@link http://php.net/sprintf sprintf()}.
883          * @param mixed $args,... further variables to substitute into the query's placeholders if being called like
884          *      {@link http://php.net/sprintf sprintf()}.
885          * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
886          *      if there was something to prepare
887          */
888         function prepare( $query = null ) { // ( $query, *$args )
889                 if ( is_null( $query ) )
890                         return;
891
892                 $args = func_get_args();
893                 array_shift( $args );
894                 // If args were passed as an array (as in vsprintf), move them up
895                 if ( isset( $args[0] ) && is_array($args[0]) )
896                         $args = $args[0];
897                 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
898                 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
899                 $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
900                 array_walk( $args, array( &$this, 'escape_by_ref' ) );
901                 return @vsprintf( $query, $args );
902         }
903
904         /**
905          * Print SQL/DB error.
906          *
907          * @since 0.71
908          * @global array $EZSQL_ERROR Stores error information of query and error string
909          *
910          * @param string $str The error to display
911          * @return bool False if the showing of errors is disabled.
912          */
913         function print_error( $str = '' ) {
914                 global $EZSQL_ERROR;
915
916                 if ( !$str )
917                         $str = mysql_error( $this->dbh );
918                 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
919
920                 if ( $this->suppress_errors )
921                         return false;
922
923                 if ( $caller = $this->get_caller() )
924                         $error_str = sprintf( /*WP_I18N_DB_QUERY_ERROR_FULL*/'WordPress database error %1$s for query %2$s made by %3$s'/*/WP_I18N_DB_QUERY_ERROR_FULL*/, $str, $this->last_query, $caller );
925                 else
926                         $error_str = sprintf( /*WP_I18N_DB_QUERY_ERROR*/'WordPress database error %1$s for query %2$s'/*/WP_I18N_DB_QUERY_ERROR*/, $str, $this->last_query );
927
928                 if ( function_exists( 'error_log' )
929                         && ( $log_file = @ini_get( 'error_log' ) )
930                         && ( 'syslog' == $log_file || @is_writable( $log_file ) )
931                         )
932                         @error_log( $error_str );
933
934                 // Are we showing errors?
935                 if ( ! $this->show_errors )
936                         return false;
937
938                 // If there is an error then take note of it
939                 if ( is_multisite() ) {
940                         $msg = "WordPress database error: [$str]\n{$this->last_query}\n";
941                         if ( defined( 'ERRORLOGFILE' ) )
942                                 error_log( $msg, 3, ERRORLOGFILE );
943                         if ( defined( 'DIEONDBERROR' ) )
944                                 wp_die( $msg );
945                 } else {
946                         $str   = htmlspecialchars( $str, ENT_QUOTES );
947                         $query = htmlspecialchars( $this->last_query, ENT_QUOTES );
948
949                         print "<div id='error'>
950                         <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
951                         <code>$query</code></p>
952                         </div>";
953                 }
954         }
955
956         /**
957          * Enables showing of database errors.
958          *
959          * This function should be used only to enable showing of errors.
960          * wpdb::hide_errors() should be used instead for hiding of errors. However,
961          * this function can be used to enable and disable showing of database
962          * errors.
963          *
964          * @since 0.71
965          * @see wpdb::hide_errors()
966          *
967          * @param bool $show Whether to show or hide errors
968          * @return bool Old value for showing errors.
969          */
970         function show_errors( $show = true ) {
971                 $errors = $this->show_errors;
972                 $this->show_errors = $show;
973                 return $errors;
974         }
975
976         /**
977          * Disables showing of database errors.
978          *
979          * By default database errors are not shown.
980          *
981          * @since 0.71
982          * @see wpdb::show_errors()
983          *
984          * @return bool Whether showing of errors was active
985          */
986         function hide_errors() {
987                 $show = $this->show_errors;
988                 $this->show_errors = false;
989                 return $show;
990         }
991
992         /**
993          * Whether to suppress database errors.
994          *
995          * By default database errors are suppressed, with a simple
996          * call to this function they can be enabled.
997          *
998          * @since 2.5
999          * @see wpdb::hide_errors()
1000          * @param bool $suppress Optional. New value. Defaults to true.
1001          * @return bool Old value
1002          */
1003         function suppress_errors( $suppress = true ) {
1004                 $errors = $this->suppress_errors;
1005                 $this->suppress_errors = (bool) $suppress;
1006                 return $errors;
1007         }
1008
1009         /**
1010          * Kill cached query results.
1011          *
1012          * @since 0.71
1013          * @return void
1014          */
1015         function flush() {
1016                 $this->last_result = array();
1017                 $this->col_info    = null;
1018                 $this->last_query  = null;
1019         }
1020
1021         function db_connect( $query = "SELECT" ) {
1022                 global $db_list, $global_db_list;
1023                 if ( ! is_array( $db_list ) )
1024                         return true;
1025
1026                 if ( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
1027                         $action = 'global';
1028                         $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ];
1029                         $this->db_global = $details;
1030                 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
1031                         $action = 'write';
1032                         $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ];
1033                         $this->db_write = $details;
1034                 } else {
1035                         $action = '';
1036                         $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ];
1037                         $this->db_read = $details;
1038                 }
1039
1040                 $dbhname = "dbh" . $action;
1041                 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
1042                 if (!$this->$dbhname ) {
1043                         $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
1044 <h1>Error establishing a database connection</h1>
1045 <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p>
1046 <ul>
1047         <li>Are you sure you have the correct username and password?</li>
1048         <li>Are you sure that you have typed the correct hostname?</li>
1049         <li>Are you sure that the database server is running?</li>
1050 </ul>
1051 <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
1052 "/*/WP_I18N_DB_CONN_ERROR*/, $details['db_host'] ), 'db_connect_fail' );
1053                 }
1054                 $this->select( $details[ 'db_name' ], $this->$dbhname );
1055         }
1056
1057         /**
1058          * Perform a MySQL database query, using current database connection.
1059          *
1060          * More information can be found on the codex page.
1061          *
1062          * @since 0.71
1063          *
1064          * @param string $query Database query
1065          * @return int|false Number of rows affected/selected or false on error
1066          */
1067         function query( $query ) {
1068                 if ( ! $this->ready )
1069                         return false;
1070
1071                 // some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
1072                 if ( function_exists( 'apply_filters' ) )
1073                         $query = apply_filters( 'query', $query );
1074
1075                 $return_val = 0;
1076                 $this->flush();
1077
1078                 // Log how the function was called
1079                 $this->func_call = "\$db->query(\"$query\")";
1080
1081                 // Keep track of the last query for debug..
1082                 $this->last_query = $query;
1083
1084                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
1085                         $this->timer_start();
1086
1087                 // use $this->dbh for read ops, and $this->dbhwrite for write ops
1088                 // use $this->dbhglobal for gloal table ops
1089                 unset( $dbh );
1090                 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) {
1091                         if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
1092                                 if( false == isset( $this->dbhglobal ) ) {
1093                                         $this->db_connect( $query );
1094                                 }
1095                                 $dbh =& $this->dbhglobal;
1096                                 $this->last_db_used = "global";
1097                         } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
1098                                 if( false == isset( $this->dbhwrite ) ) {
1099                                         $this->db_connect( $query );
1100                                 }
1101                                 $dbh =& $this->dbhwrite;
1102                                 $this->last_db_used = "write";
1103                         } else {
1104                                 $dbh =& $this->dbh;
1105                                 $this->last_db_used = "read";
1106                         }
1107                 } else {
1108                         $dbh =& $this->dbh;
1109                         $this->last_db_used = "other/read";
1110                 }
1111
1112                 $this->result = @mysql_query( $query, $dbh );
1113                 $this->num_queries++;
1114
1115                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
1116                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
1117
1118                 // If there is an error then take note of it..
1119                 if ( $this->last_error = mysql_error( $dbh ) ) {
1120                         $this->print_error();
1121                         return false;
1122                 }
1123
1124                 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
1125                         $this->rows_affected = mysql_affected_rows( $dbh );
1126                         // Take note of the insert_id
1127                         if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
1128                                 $this->insert_id = mysql_insert_id($dbh);
1129                         }
1130                         // Return number of rows affected
1131                         $return_val = $this->rows_affected;
1132                 } else {
1133                         $i = 0;
1134                         while ( $i < @mysql_num_fields( $this->result ) ) {
1135                                 $this->col_info[$i] = @mysql_fetch_field( $this->result );
1136                                 $i++;
1137                         }
1138                         $num_rows = 0;
1139                         while ( $row = @mysql_fetch_object( $this->result ) ) {
1140                                 $this->last_result[$num_rows] = $row;
1141                                 $num_rows++;
1142                         }
1143
1144                         @mysql_free_result( $this->result );
1145
1146                         // Log number of rows the query returned
1147                         // and return number of rows selected
1148                         $this->num_rows = $num_rows;
1149                         $return_val     = $num_rows;
1150                 }
1151
1152                 return $return_val;
1153         }
1154
1155         /**
1156          * Insert a row into a table.
1157          *
1158          * <code>
1159          * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
1160          * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
1161          * </code>
1162          *
1163          * @since 2.5.0
1164          * @see wpdb::prepare()
1165          * @see wpdb::$field_types
1166          * @see wp_set_wpdb_vars()
1167          *
1168          * @param string $table table name
1169          * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
1170          * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
1171          *      A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
1172          * @return int|false The number of rows inserted, or false on error.
1173          */
1174         function insert( $table, $data, $format = null ) {
1175                 return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
1176         }
1177
1178         /**
1179          * Replace a row into a table.
1180          *
1181          * <code>
1182          * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
1183          * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
1184          * </code>
1185          *
1186          * @since 3.0.0
1187          * @see wpdb::prepare()
1188          * @see wpdb::$field_types
1189          * @see wp_set_wpdb_vars()
1190          *
1191          * @param string $table table name
1192          * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
1193          * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
1194          *      A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
1195          * @return int|false The number of rows affected, or false on error.
1196          */
1197         function replace( $table, $data, $format = null ) {
1198                 return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
1199         }
1200
1201         /**
1202          * Helper function for insert and replace.
1203          *
1204          * Runs an insert or replace query based on $type argument.
1205          *
1206          * @access private
1207          * @since 3.0.0
1208          * @see wpdb::prepare()
1209          * @see wpdb::$field_types
1210          * @see wp_set_wpdb_vars()
1211          *
1212          * @param string $table table name
1213          * @param array $data Data to insert (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
1214          * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
1215          *      A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
1216          * @return int|false The number of rows affected, or false on error.
1217          */
1218         function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
1219                 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
1220                         return false;
1221                 $formats = $format = (array) $format;
1222                 $fields = array_keys( $data );
1223                 $formatted_fields = array();
1224                 foreach ( $fields as $field ) {
1225                         if ( !empty( $format ) )
1226                                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
1227                         elseif ( isset( $this->field_types[$field] ) )
1228                                 $form = $this->field_types[$field];
1229                         else
1230                                 $form = '%s';
1231                         $formatted_fields[] = $form;
1232                 }
1233                 $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
1234                 return $this->query( $this->prepare( $sql, $data ) );
1235         }
1236
1237         /**
1238          * Update a row in the table
1239          *
1240          * <code>
1241          * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
1242          * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
1243          * </code>
1244          *
1245          * @since 2.5.0
1246          * @see wpdb::prepare()
1247          * @see wpdb::$field_types
1248          * @see wp_set_wpdb_vars()
1249          *
1250          * @param string $table table name
1251          * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
1252          * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
1253          * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data.
1254          *      A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
1255          * @param array|string $format_where Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where.  A format is one of '%d', '%s' (decimal number, string).  If omitted, all values in $where will be treated as strings.
1256          * @return int|false The number of rows updated, or false on error.
1257          */
1258         function update( $table, $data, $where, $format = null, $where_format = null ) {
1259                 if ( ! is_array( $data ) || ! is_array( $where ) )
1260                         return false;
1261
1262                 $formats = $format = (array) $format;
1263                 $bits = $wheres = array();
1264                 foreach ( (array) array_keys( $data ) as $field ) {
1265                         if ( !empty( $format ) )
1266                                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
1267                         elseif ( isset($this->field_types[$field]) )
1268                                 $form = $this->field_types[$field];
1269                         else
1270                                 $form = '%s';
1271                         $bits[] = "`$field` = {$form}";
1272                 }
1273
1274                 $where_formats = $where_format = (array) $where_format;
1275                 foreach ( (array) array_keys( $where ) as $field ) {
1276                         if ( !empty( $where_format ) )
1277                                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
1278                         elseif ( isset( $this->field_types[$field] ) )
1279                                 $form = $this->field_types[$field];
1280                         else
1281                                 $form = '%s';
1282                         $wheres[] = "`$field` = {$form}";
1283                 }
1284
1285                 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
1286                 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
1287         }
1288
1289         /**
1290          * Retrieve one variable from the database.
1291          *
1292          * Executes a SQL query and returns the value from the SQL result.
1293          * If the SQL result contains more than one column and/or more than one row, this function returns the value in the column and row specified.
1294          * If $query is null, this function returns the value in the specified column and row from the previous SQL result.
1295          *
1296          * @since 0.71
1297          *
1298          * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
1299          * @param int $x Optional. Column of value to return.  Indexed from 0.
1300          * @param int $y Optional. Row of value to return.  Indexed from 0.
1301          * @return string|null Database query result (as string), or null on failure
1302          */
1303         function get_var( $query = null, $x = 0, $y = 0 ) {
1304                 $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
1305                 if ( $query )
1306                         $this->query( $query );
1307
1308                 // Extract var out of cached results based x,y vals
1309                 if ( !empty( $this->last_result[$y] ) ) {
1310                         $values = array_values( get_object_vars( $this->last_result[$y] ) );
1311                 }
1312
1313                 // If there is a value return it else return null
1314                 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
1315         }
1316
1317         /**
1318          * Retrieve one row from the database.
1319          *
1320          * Executes a SQL query and returns the row from the SQL result.
1321          *
1322          * @since 0.71
1323          *
1324          * @param string|null $query SQL query.
1325          * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...),
1326          *      a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
1327          * @param int $y Optional. Row to return. Indexed from 0.
1328          * @return mixed Database query result in format specifed by $output or null on failure
1329          */
1330         function get_row( $query = null, $output = OBJECT, $y = 0 ) {
1331                 $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
1332                 if ( $query )
1333                         $this->query( $query );
1334                 else
1335                         return null;
1336
1337                 if ( !isset( $this->last_result[$y] ) )
1338                         return null;
1339
1340                 if ( $output == OBJECT ) {
1341                         return $this->last_result[$y] ? $this->last_result[$y] : null;
1342                 } elseif ( $output == ARRAY_A ) {
1343                         return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
1344                 } elseif ( $output == ARRAY_N ) {
1345                         return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
1346                 } else {
1347                         $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/);
1348                 }
1349         }
1350
1351         /**
1352          * Retrieve one column from the database.
1353          *
1354          * Executes a SQL query and returns the column from the SQL result.
1355          * If the SQL result contains more than one column, this function returns the column specified.
1356          * If $query is null, this function returns the specified column from the previous SQL result.
1357          *
1358          * @since 0.71
1359          *
1360          * @param string|null $query Optional. SQL query. Defaults to previous query.
1361          * @param int $x Optional. Column to return. Indexed from 0.
1362          * @return array Database query result. Array indexed from 0 by SQL result row number.
1363          */
1364         function get_col( $query = null , $x = 0 ) {
1365                 if ( $query )
1366                         $this->query( $query );
1367
1368                 $new_array = array();
1369                 // Extract the column values
1370                 for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
1371                         $new_array[$i] = $this->get_var( null, $x, $i );
1372                 }
1373                 return $new_array;
1374         }
1375
1376         /**
1377          * Retrieve an entire SQL result set from the database (i.e., many rows)
1378          *
1379          * Executes a SQL query and returns the entire SQL result.
1380          *
1381          * @since 0.71
1382          *
1383          * @param string $query SQL query.
1384          * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number.
1385          *      Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively.
1386          *      With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value.  Duplicate keys are discarded.
1387          * @return mixed Database query results
1388          */
1389         function get_results( $query = null, $output = OBJECT ) {
1390                 $this->func_call = "\$db->get_results(\"$query\", $output)";
1391
1392                 if ( $query )
1393                         $this->query( $query );
1394                 else
1395                         return null;
1396
1397                 $new_array = array();
1398                 if ( $output == OBJECT ) {
1399                         // Return an integer-keyed array of row objects
1400                         return $this->last_result;
1401                 } elseif ( $output == OBJECT_K ) {
1402                         // Return an array of row objects with keys from column 1
1403                         // (Duplicates are discarded)
1404                         foreach ( $this->last_result as $row ) {
1405                                 $key = array_shift( get_object_vars( $row ) );
1406                                 if ( ! isset( $new_array[ $key ] ) )
1407                                         $new_array[ $key ] = $row;
1408                         }
1409                         return $new_array;
1410                 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
1411                         // Return an integer-keyed array of...
1412                         if ( $this->last_result ) {
1413                                 foreach( (array) $this->last_result as $row ) {
1414                                         if ( $output == ARRAY_N ) {
1415                                                 // ...integer-keyed row arrays
1416                                                 $new_array[] = array_values( get_object_vars( $row ) );
1417                                         } else {
1418                                                 // ...column name-keyed row arrays
1419                                                 $new_array[] = get_object_vars( $row );
1420                                         }
1421                                 }
1422                         }
1423                         return $new_array;
1424                 }
1425                 return null;
1426         }
1427
1428         /**
1429          * Retrieve column metadata from the last query.
1430          *
1431          * @since 0.71
1432          *
1433          * @param string $info_type Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
1434          * @param int $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
1435          * @return mixed Column Results
1436          */
1437         function get_col_info( $info_type = 'name', $col_offset = -1 ) {
1438                 if ( $this->col_info ) {
1439                         if ( $col_offset == -1 ) {
1440                                 $i = 0;
1441                                 $new_array = array();
1442                                 foreach( (array) $this->col_info as $col ) {
1443                                         $new_array[$i] = $col->{$info_type};
1444                                         $i++;
1445                                 }
1446                                 return $new_array;
1447                         } else {
1448                                 return $this->col_info[$col_offset]->{$info_type};
1449                         }
1450                 }
1451         }
1452
1453         /**
1454          * Starts the timer, for debugging purposes.
1455          *
1456          * @since 1.5.0
1457          *
1458          * @return true
1459          */
1460         function timer_start() {
1461                 $mtime            = explode( ' ', microtime() );
1462                 $this->time_start = $mtime[1] + $mtime[0];
1463                 return true;
1464         }
1465
1466         /**
1467          * Stops the debugging timer.
1468          *
1469          * @since 1.5.0
1470          *
1471          * @return int Total time spent on the query, in milliseconds
1472          */
1473         function timer_stop() {
1474                 $mtime      = explode( ' ', microtime() );
1475                 $time_end   = $mtime[1] + $mtime[0];
1476                 $time_total = $time_end - $this->time_start;
1477                 return $time_total;
1478         }
1479
1480         /**
1481          * Wraps errors in a nice header and footer and dies.
1482          *
1483          * Will not die if wpdb::$show_errors is true
1484          *
1485          * @since 1.5.0
1486          *
1487          * @param string $message The Error message
1488          * @param string $error_code Optional. A Computer readable string to identify the error.
1489          * @return false|void
1490          */
1491         function bail( $message, $error_code = '500' ) {
1492                 if ( !$this->show_errors ) {
1493                         if ( class_exists( 'WP_Error' ) )
1494                                 $this->error = new WP_Error($error_code, $message);
1495                         else
1496                                 $this->error = $message;
1497                         return false;
1498                 }
1499                 wp_die($message);
1500         }
1501
1502         /**
1503          * Whether MySQL database is at least the required minimum version.
1504          *
1505          * @since 2.5.0
1506          * @uses $wp_version
1507          * @uses $required_mysql_version
1508          *
1509          * @return WP_Error
1510          */
1511         function check_database_version() {
1512                 global $wp_version, $required_mysql_version;
1513                 // Make sure the server has the required MySQL version
1514                 if ( version_compare($this->db_version(), $required_mysql_version, '<') )
1515                         return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
1516         }
1517
1518         /**
1519          * Whether the database supports collation.
1520          *
1521          * Called when WordPress is generating the table scheme.
1522          *
1523          * @since 2.5.0
1524          *
1525          * @return bool True if collation is supported, false if version does not
1526          */
1527         function supports_collation() {
1528                 return $this->has_cap( 'collation' );
1529         }
1530
1531         /**
1532          * Determine if a database supports a particular feature
1533          *
1534          * @since 2.7
1535          * @see   wpdb::db_version()
1536          *
1537          * @param string $db_cap the feature
1538          * @return bool
1539          */
1540         function has_cap( $db_cap ) {
1541                 $version = $this->db_version();
1542
1543                 switch ( strtolower( $db_cap ) ) {
1544                         case 'collation' :    // @since 2.5.0
1545                         case 'group_concat' : // @since 2.7
1546                         case 'subqueries' :   // @since 2.7
1547                                 return version_compare( $version, '4.1', '>=' );
1548                 };
1549
1550                 return false;
1551         }
1552
1553         /**
1554          * Retrieve the name of the function that called wpdb.
1555          *
1556          * Searches up the list of functions until it reaches
1557          * the one that would most logically had called this method.
1558          *
1559          * @since 2.5.0
1560          *
1561          * @return string The name of the calling function
1562          */
1563         function get_caller() {
1564                 $trace  = array_reverse( debug_backtrace() );
1565                 $caller = array();
1566
1567                 foreach ( $trace as $call ) {
1568                         if ( isset( $call['class'] ) && __CLASS__ == $call['class'] )
1569                                 continue; // Filter out wpdb calls.
1570                         $caller[] = isset( $call['class'] ) ? "{$call['class']}->{$call['function']}" : $call['function'];
1571                 }
1572
1573                 return join( ', ', $caller );
1574         }
1575
1576         /**
1577          * The database version number.
1578          *
1579          * @return false|string false on failure, version number on success
1580          */
1581         function db_version() {
1582                 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
1583         }
1584 }
1585
1586 if ( ! isset( $wpdb ) ) {
1587         /**
1588          * WordPress Database Object, if it isn't set already in wp-content/db.php
1589          * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database
1590          * @since 0.71
1591          */
1592         $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
1593 }
1594 ?>