[ Index ]

PHP Cross Reference of WordPress 3.0 beta 1

[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/wp-includes/ -> wp-db.php (source)

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


Generated: Mon Apr 5 14:26:09 2010 Cross-referenced by PHPXref 0.7