[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

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


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting