| [ Index ] |
PHP Cross Reference of Wordpress 2.9.1 |
[Summary view] [Print] [Text view]
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 {@internal Version Unknown}} 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 {@internal Version Unknown}} 69 * @var bool 70 */ 71 var $suppress_errors = false; 72 73 /** 74 * The last error during query. 75 * 76 * @since {@internal Version Unknown}} 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 * Saved result of the last query made 92 * 93 * @since 1.2.0 94 * @access private 95 * @var array 96 */ 97 var $last_query; 98 99 /** 100 * Saved info on the table column 101 * 102 * @since 1.2.0 103 * @access private 104 * @var array 105 */ 106 var $col_info; 107 108 /** 109 * Saved queries that were executed 110 * 111 * @since 1.5.0 112 * @access private 113 * @var array 114 */ 115 var $queries; 116 117 /** 118 * WordPress table prefix 119 * 120 * You can set this to have multiple WordPress installations 121 * in a single database. The second reason is for possible 122 * security precautions. 123 * 124 * @since 0.71 125 * @access private 126 * @var string 127 */ 128 var $prefix = ''; 129 130 /** 131 * Whether the database queries are ready to start executing. 132 * 133 * @since 2.5.0 134 * @access private 135 * @var bool 136 */ 137 var $ready = false; 138 139 /** 140 * WordPress Posts table 141 * 142 * @since 1.5.0 143 * @access public 144 * @var string 145 */ 146 var $posts; 147 148 /** 149 * WordPress Users table 150 * 151 * @since 1.5.0 152 * @access public 153 * @var string 154 */ 155 var $users; 156 157 /** 158 * WordPress Categories table 159 * 160 * @since 1.5.0 161 * @access public 162 * @var string 163 */ 164 var $categories; 165 166 /** 167 * WordPress Post to Category table 168 * 169 * @since 1.5.0 170 * @access public 171 * @var string 172 */ 173 var $post2cat; 174 175 /** 176 * WordPress Comments table 177 * 178 * @since 1.5.0 179 * @access public 180 * @var string 181 */ 182 var $comments; 183 184 /** 185 * WordPress Links table 186 * 187 * @since 1.5.0 188 * @access public 189 * @var string 190 */ 191 var $links; 192 193 /** 194 * WordPress Options table 195 * 196 * @since 1.5.0 197 * @access public 198 * @var string 199 */ 200 var $options; 201 202 /** 203 * WordPress Post Metadata table 204 * 205 * @since {@internal Version Unknown}} 206 * @access public 207 * @var string 208 */ 209 var $postmeta; 210 211 /** 212 * WordPress Comment Metadata table 213 * 214 * @since 2.9 215 * @access public 216 * @var string 217 */ 218 var $commentmeta; 219 220 /** 221 * WordPress User Metadata table 222 * 223 * @since 2.3.0 224 * @access public 225 * @var string 226 */ 227 var $usermeta; 228 229 /** 230 * WordPress Terms table 231 * 232 * @since 2.3.0 233 * @access public 234 * @var string 235 */ 236 var $terms; 237 238 /** 239 * WordPress Term Taxonomy table 240 * 241 * @since 2.3.0 242 * @access public 243 * @var string 244 */ 245 var $term_taxonomy; 246 247 /** 248 * WordPress Term Relationships table 249 * 250 * @since 2.3.0 251 * @access public 252 * @var string 253 */ 254 var $term_relationships; 255 256 /** 257 * List of WordPress tables 258 * 259 * @since {@internal Version Unknown}} 260 * @access private 261 * @var array 262 */ 263 var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options', 264 'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta'); 265 266 /** 267 * List of deprecated WordPress tables 268 * 269 * @since 2.9.0 270 * @access private 271 * @var array 272 */ 273 var $old_tables = array('categories', 'post2cat', 'link2cat'); 274 275 276 /** 277 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php. 278 * 279 * Keys are colmn names, values are format types: 'ID' => '%d' 280 * 281 * @since 2.8.0 282 * @see wpdb:prepare() 283 * @see wpdb:insert() 284 * @see wpdb:update() 285 * @access public 286 * @war array 287 */ 288 var $field_types = array(); 289 290 /** 291 * Database table columns charset 292 * 293 * @since 2.2.0 294 * @access public 295 * @var string 296 */ 297 var $charset; 298 299 /** 300 * Database table columns collate 301 * 302 * @since 2.2.0 303 * @access public 304 * @var string 305 */ 306 var $collate; 307 308 /** 309 * Whether to use mysql_real_escape_string 310 * 311 * @since 2.8.0 312 * @access public 313 * @var bool 314 */ 315 var $real_escape = false; 316 317 /** 318 * Database Username 319 * 320 * @since 2.9.0 321 * @access private 322 * @var string 323 */ 324 var $dbuser; 325 326 /** 327 * Connects to the database server and selects a database 328 * 329 * PHP4 compatibility layer for calling the PHP5 constructor. 330 * 331 * @uses wpdb::__construct() Passes parameters and returns result 332 * @since 0.71 333 * 334 * @param string $dbuser MySQL database user 335 * @param string $dbpassword MySQL database password 336 * @param string $dbname MySQL database name 337 * @param string $dbhost MySQL database host 338 */ 339 function wpdb($dbuser, $dbpassword, $dbname, $dbhost) { 340 return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost); 341 } 342 343 /** 344 * Connects to the database server and selects a database 345 * 346 * PHP5 style constructor for compatibility with PHP5. Does 347 * the actual setting up of the class properties and connection 348 * to the database. 349 * 350 * @since 2.0.8 351 * 352 * @param string $dbuser MySQL database user 353 * @param string $dbpassword MySQL database password 354 * @param string $dbname MySQL database name 355 * @param string $dbhost MySQL database host 356 */ 357 function __construct($dbuser, $dbpassword, $dbname, $dbhost) { 358 register_shutdown_function(array(&$this, "__destruct")); 359 360 if ( WP_DEBUG ) 361 $this->show_errors(); 362 363 if ( defined('DB_CHARSET') ) 364 $this->charset = DB_CHARSET; 365 366 if ( defined('DB_COLLATE') ) 367 $this->collate = DB_COLLATE; 368 369 $this->dbuser = $dbuser; 370 371 $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true); 372 if (!$this->dbh) { 373 $this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/" 374 <h1>Error establishing a database connection</h1> 375 <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> 376 <ul> 377 <li>Are you sure you have the correct username and password?</li> 378 <li>Are you sure that you have typed the correct hostname?</li> 379 <li>Are you sure that the database server is running?</li> 380 </ul> 381 <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> 382 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost), 'db_connect_fail'); 383 return; 384 } 385 386 $this->ready = true; 387 388 if ( $this->has_cap( 'collation' ) && !empty($this->charset) ) { 389 if ( function_exists('mysql_set_charset') ) { 390 mysql_set_charset($this->charset, $this->dbh); 391 $this->real_escape = true; 392 } else { 393 $collation_query = "SET NAMES '{$this->charset}'"; 394 if ( !empty($this->collate) ) 395 $collation_query .= " COLLATE '{$this->collate}'"; 396 $this->query($collation_query); 397 } 398 } 399 400 $this->select($dbname); 401 } 402 403 /** 404 * PHP5 style destructor and will run when database object is destroyed. 405 * 406 * @since 2.0.8 407 * 408 * @return bool Always true 409 */ 410 function __destruct() { 411 return true; 412 } 413 414 /** 415 * Sets the table prefix for the WordPress tables. 416 * 417 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to 418 * override the WordPress users and usersmeta tables that would otherwise be determined by the $prefix. 419 * 420 * @since 2.5.0 421 * 422 * @param string $prefix Alphanumeric name for the new prefix. 423 * @return string|WP_Error Old prefix or WP_Error on error 424 */ 425 function set_prefix($prefix) { 426 427 if ( preg_match('|[^a-z0-9_]|i', $prefix) ) 428 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); 429 430 $old_prefix = $this->prefix; 431 $this->prefix = $prefix; 432 433 foreach ( (array) $this->tables as $table ) 434 $this->$table = $this->prefix . $table; 435 436 if ( defined('CUSTOM_USER_TABLE') ) 437 $this->users = CUSTOM_USER_TABLE; 438 439 if ( defined('CUSTOM_USER_META_TABLE') ) 440 $this->usermeta = CUSTOM_USER_META_TABLE; 441 442 return $old_prefix; 443 } 444 445 /** 446 * Selects a database using the current database connection. 447 * 448 * The database name will be changed based on the current database 449 * connection. On failure, the execution will bail and display an DB error. 450 * 451 * @since 0.71 452 * 453 * @param string $db MySQL database name 454 * @return null Always null. 455 */ 456 function select($db) { 457 if (!@mysql_select_db($db, $this->dbh)) { 458 $this->ready = false; 459 $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/' 460 <h1>Can’t select database</h1> 461 <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> 462 <ul> 463 <li>Are you sure it exists?</li> 464 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> 465 <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> 466 </ul> 467 <p>If you don\'t know how to setup 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'); 468 return; 469 } 470 } 471 472 function _weak_escape($string) { 473 return addslashes($string); 474 } 475 476 function _real_escape($string) { 477 if ( $this->dbh && $this->real_escape ) 478 return mysql_real_escape_string( $string, $this->dbh ); 479 else 480 return addslashes( $string ); 481 } 482 483 function _escape($data) { 484 if ( is_array($data) ) { 485 foreach ( (array) $data as $k => $v ) { 486 if ( is_array($v) ) 487 $data[$k] = $this->_escape( $v ); 488 else 489 $data[$k] = $this->_real_escape( $v ); 490 } 491 } else { 492 $data = $this->_real_escape( $data ); 493 } 494 495 return $data; 496 } 497 498 /** 499 * Escapes content for insertion into the database using addslashes(), for security 500 * 501 * @since 0.71 502 * 503 * @param string|array $data 504 * @return string query safe string 505 */ 506 function escape($data) { 507 if ( is_array($data) ) { 508 foreach ( (array) $data as $k => $v ) { 509 if ( is_array($v) ) 510 $data[$k] = $this->escape( $v ); 511 else 512 $data[$k] = $this->_weak_escape( $v ); 513 } 514 } else { 515 $data = $this->_weak_escape( $data ); 516 } 517 518 return $data; 519 } 520 521 /** 522 * Escapes content by reference for insertion into the database, for security 523 * 524 * @since 2.3.0 525 * 526 * @param string $s 527 */ 528 function escape_by_ref(&$string) { 529 $string = $this->_real_escape( $string ); 530 } 531 532 /** 533 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 534 * 535 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). 536 * Does not support sign, padding, alignment, width or precision specifiers. 537 * Does not support argument numbering/swapping. 538 * 539 * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}. 540 * 541 * Both %d and %s should be left unquoted in the query string. 542 * 543 * <code> 544 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) 545 * </code> 546 * 547 * @link http://php.net/sprintf Description of syntax. 548 * @since 2.3.0 549 * 550 * @param string $query Query statement with sprintf()-like placeholders 551 * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. 552 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. 553 * @return null|string Sanitized query string 554 */ 555 function prepare($query = null) { // ( $query, *$args ) 556 if ( is_null( $query ) ) 557 return; 558 $args = func_get_args(); 559 array_shift($args); 560 // If args were passed as an array (as in vsprintf), move them up 561 if ( isset($args[0]) && is_array($args[0]) ) 562 $args = $args[0]; 563 $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it 564 $query = str_replace('"%s"', '%s', $query); // doublequote unquoting 565 $query = str_replace('%s', "'%s'", $query); // quote the strings 566 array_walk($args, array(&$this, 'escape_by_ref')); 567 return @vsprintf($query, $args); 568 } 569 570 /** 571 * Print SQL/DB error. 572 * 573 * @since 0.71 574 * @global array $EZSQL_ERROR Stores error information of query and error string 575 * 576 * @param string $str The error to display 577 * @return bool False if the showing of errors is disabled. 578 */ 579 function print_error($str = '') { 580 global $EZSQL_ERROR; 581 582 if (!$str) $str = mysql_error($this->dbh); 583 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str); 584 585 if ( $this->suppress_errors ) 586 return false; 587 588 if ( $caller = $this->get_caller() ) 589 $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); 590 else 591 $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); 592 593 $log_error = true; 594 if ( ! function_exists('error_log') ) 595 $log_error = false; 596 597 $log_file = @ini_get('error_log'); 598 if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) ) 599 $log_error = false; 600 601 if ( $log_error ) 602 @error_log($error_str, 0); 603 604 // Is error output turned on or not.. 605 if ( !$this->show_errors ) 606 return false; 607 608 $str = htmlspecialchars($str, ENT_QUOTES); 609 $query = htmlspecialchars($this->last_query, ENT_QUOTES); 610 611 // If there is an error then take note of it 612 print "<div id='error'> 613 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> 614 <code>$query</code></p> 615 </div>"; 616 } 617 618 /** 619 * Enables showing of database errors. 620 * 621 * This function should be used only to enable showing of errors. 622 * wpdb::hide_errors() should be used instead for hiding of errors. However, 623 * this function can be used to enable and disable showing of database 624 * errors. 625 * 626 * @since 0.71 627 * 628 * @param bool $show Whether to show or hide errors 629 * @return bool Old value for showing errors. 630 */ 631 function show_errors( $show = true ) { 632 $errors = $this->show_errors; 633 $this->show_errors = $show; 634 return $errors; 635 } 636 637 /** 638 * Disables showing of database errors. 639 * 640 * @since 0.71 641 * 642 * @return bool Whether showing of errors was active or not 643 */ 644 function hide_errors() { 645 $show = $this->show_errors; 646 $this->show_errors = false; 647 return $show; 648 } 649 650 /** 651 * Whether to suppress database errors. 652 * 653 * @param unknown_type $suppress 654 * @return unknown 655 */ 656 function suppress_errors( $suppress = true ) { 657 $errors = $this->suppress_errors; 658 $this->suppress_errors = $suppress; 659 return $errors; 660 } 661 662 /** 663 * Kill cached query results. 664 * 665 * @since 0.71 666 */ 667 function flush() { 668 $this->last_result = array(); 669 $this->col_info = null; 670 $this->last_query = null; 671 } 672 673 /** 674 * Perform a MySQL database query, using current database connection. 675 * 676 * More information can be found on the codex page. 677 * 678 * @since 0.71 679 * 680 * @param string $query 681 * @return int|false Number of rows affected/selected or false on error 682 */ 683 function query($query) { 684 if ( ! $this->ready ) 685 return false; 686 687 // filter the query, if filters are available 688 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method 689 if ( function_exists('apply_filters') ) 690 $query = apply_filters('query', $query); 691 692 // initialise return 693 $return_val = 0; 694 $this->flush(); 695 696 // Log how the function was called 697 $this->func_call = "\$db->query(\"$query\")"; 698 699 // Keep track of the last query for debug.. 700 $this->last_query = $query; 701 702 // Perform the query via std mysql_query function.. 703 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 704 $this->timer_start(); 705 706 $this->result = @mysql_query($query, $this->dbh); 707 ++$this->num_queries; 708 709 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 710 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 711 712 // If there is an error then take note of it.. 713 if ( $this->last_error = mysql_error($this->dbh) ) { 714 $this->print_error(); 715 return false; 716 } 717 718 if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) { 719 $this->rows_affected = mysql_affected_rows($this->dbh); 720 // Take note of the insert_id 721 if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { 722 $this->insert_id = mysql_insert_id($this->dbh); 723 } 724 // Return number of rows affected 725 $return_val = $this->rows_affected; 726 } else { 727 $i = 0; 728 while ($i < @mysql_num_fields($this->result)) { 729 $this->col_info[$i] = @mysql_fetch_field($this->result); 730 $i++; 731 } 732 $num_rows = 0; 733 while ( $row = @mysql_fetch_object($this->result) ) { 734 $this->last_result[$num_rows] = $row; 735 $num_rows++; 736 } 737 738 @mysql_free_result($this->result); 739 740 // Log number of rows the query returned 741 $this->num_rows = $num_rows; 742 743 // Return number of rows selected 744 $return_val = $this->num_rows; 745 } 746 747 return $return_val; 748 } 749 750 /** 751 * Insert a row into a table. 752 * 753 * <code> 754 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 755 * </code> 756 * 757 * @since 2.5.0 758 * @see wpdb::prepare() 759 * 760 * @param string $table table name 761 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 762 * @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. A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings. 763 * @return int|false The number of rows inserted, or false on error. 764 */ 765 function insert($table, $data, $format = null) { 766 $formats = $format = (array) $format; 767 $fields = array_keys($data); 768 $formatted_fields = array(); 769 foreach ( $fields as $field ) { 770 if ( !empty($format) ) 771 $form = ( $form = array_shift($formats) ) ? $form : $format[0]; 772 elseif ( isset($this->field_types[$field]) ) 773 $form = $this->field_types[$field]; 774 else 775 $form = '%s'; 776 $formatted_fields[] = $form; 777 } 778 $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; 779 return $this->query( $this->prepare( $sql, $data) ); 780 } 781 782 783 /** 784 * Update a row in the table 785 * 786 * <code> 787 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) 788 * </code> 789 * 790 * @since 2.5.0 791 * @see wpdb::prepare() 792 * 793 * @param string $table table name 794 * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 795 * @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". 796 * @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. A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings. 797 * @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. 798 * @return int|false The number of rows updated, or false on error. 799 */ 800 function update($table, $data, $where, $format = null, $where_format = null) { 801 if ( !is_array( $where ) ) 802 return false; 803 804 $formats = $format = (array) $format; 805 $bits = $wheres = array(); 806 foreach ( (array) array_keys($data) as $field ) { 807 if ( !empty($format) ) 808 $form = ( $form = array_shift($formats) ) ? $form : $format[0]; 809 elseif ( isset($this->field_types[$field]) ) 810 $form = $this->field_types[$field]; 811 else 812 $form = '%s'; 813 $bits[] = "`$field` = {$form}"; 814 } 815 816 $where_formats = $where_format = (array) $where_format; 817 foreach ( (array) array_keys($where) as $field ) { 818 if ( !empty($where_format) ) 819 $form = ( $form = array_shift($where_formats) ) ? $form : $where_format[0]; 820 elseif ( isset($this->field_types[$field]) ) 821 $form = $this->field_types[$field]; 822 else 823 $form = '%s'; 824 $wheres[] = "`$field` = {$form}"; 825 } 826 827 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); 828 return $this->query( $this->prepare( $sql, array_merge(array_values($data), array_values($where))) ); 829 } 830 831 /** 832 * Retrieve one variable from the database. 833 * 834 * Executes a SQL query and returns the value from the SQL result. 835 * 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. 836 * If $query is null, this function returns the value in the specified column and row from the previous SQL result. 837 * 838 * @since 0.71 839 * 840 * @param string|null $query SQL query. If null, use the result from the previous query. 841 * @param int $x (optional) Column of value to return. Indexed from 0. 842 * @param int $y (optional) Row of value to return. Indexed from 0. 843 * @return string Database query result 844 */ 845 function get_var($query=null, $x = 0, $y = 0) { 846 $this->func_call = "\$db->get_var(\"$query\",$x,$y)"; 847 if ( $query ) 848 $this->query($query); 849 850 // Extract var out of cached results based x,y vals 851 if ( !empty( $this->last_result[$y] ) ) { 852 $values = array_values(get_object_vars($this->last_result[$y])); 853 } 854 855 // If there is a value return it else return null 856 return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null; 857 } 858 859 /** 860 * Retrieve one row from the database. 861 * 862 * Executes a SQL query and returns the row from the SQL result. 863 * 864 * @since 0.71 865 * 866 * @param string|null $query SQL query. 867 * @param string $output (optional) one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...), a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively. 868 * @param int $y (optional) Row to return. Indexed from 0. 869 * @return mixed Database query result in format specifed by $output 870 */ 871 function get_row($query = null, $output = OBJECT, $y = 0) { 872 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 873 if ( $query ) 874 $this->query($query); 875 else 876 return null; 877 878 if ( !isset($this->last_result[$y]) ) 879 return null; 880 881 if ( $output == OBJECT ) { 882 return $this->last_result[$y] ? $this->last_result[$y] : null; 883 } elseif ( $output == ARRAY_A ) { 884 return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null; 885 } elseif ( $output == ARRAY_N ) { 886 return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null; 887 } else { 888 $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*/); 889 } 890 } 891 892 /** 893 * Retrieve one column from the database. 894 * 895 * Executes a SQL query and returns the column from the SQL result. 896 * If the SQL result contains more than one column, this function returns the column specified. 897 * If $query is null, this function returns the specified column from the previous SQL result. 898 * 899 * @since 0.71 900 * 901 * @param string|null $query SQL query. If null, use the result from the previous query. 902 * @param int $x Column to return. Indexed from 0. 903 * @return array Database query result. Array indexed from 0 by SQL result row number. 904 */ 905 function get_col($query = null , $x = 0) { 906 if ( $query ) 907 $this->query($query); 908 909 $new_array = array(); 910 // Extract the column values 911 for ( $i=0; $i < count($this->last_result); $i++ ) { 912 $new_array[$i] = $this->get_var(null, $x, $i); 913 } 914 return $new_array; 915 } 916 917 /** 918 * Retrieve an entire SQL result set from the database (i.e., many rows) 919 * 920 * Executes a SQL query and returns the entire SQL result. 921 * 922 * @since 0.71 923 * 924 * @param string $query SQL query. 925 * @param string $output (optional) ane 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. Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively. 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. 926 * @return mixed Database query results 927 */ 928 function get_results($query = null, $output = OBJECT) { 929 $this->func_call = "\$db->get_results(\"$query\", $output)"; 930 931 if ( $query ) 932 $this->query($query); 933 else 934 return null; 935 936 if ( $output == OBJECT ) { 937 // Return an integer-keyed array of row objects 938 return $this->last_result; 939 } elseif ( $output == OBJECT_K ) { 940 // Return an array of row objects with keys from column 1 941 // (Duplicates are discarded) 942 foreach ( $this->last_result as $row ) { 943 $key = array_shift( get_object_vars( $row ) ); 944 if ( !isset( $new_array[ $key ] ) ) 945 $new_array[ $key ] = $row; 946 } 947 return $new_array; 948 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { 949 // Return an integer-keyed array of... 950 if ( $this->last_result ) { 951 $i = 0; 952 foreach( (array) $this->last_result as $row ) { 953 if ( $output == ARRAY_N ) { 954 // ...integer-keyed row arrays 955 $new_array[$i] = array_values( get_object_vars( $row ) ); 956 } else { 957 // ...column name-keyed row arrays 958 $new_array[$i] = get_object_vars( $row ); 959 } 960 ++$i; 961 } 962 return $new_array; 963 } 964 } 965 } 966 967 /** 968 * Retrieve column metadata from the last query. 969 * 970 * @since 0.71 971 * 972 * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill 973 * @param int $col_offset 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 974 * @return mixed Column Results 975 */ 976 function get_col_info($info_type = 'name', $col_offset = -1) { 977 if ( $this->col_info ) { 978 if ( $col_offset == -1 ) { 979 $i = 0; 980 foreach( (array) $this->col_info as $col ) { 981 $new_array[$i] = $col->{$info_type}; 982 $i++; 983 } 984 return $new_array; 985 } else { 986 return $this->col_info[$col_offset]->{$info_type}; 987 } 988 } 989 } 990 991 /** 992 * Starts the timer, for debugging purposes. 993 * 994 * @since 1.5.0 995 * 996 * @return true 997 */ 998 function timer_start() { 999 $mtime = microtime(); 1000 $mtime = explode(' ', $mtime); 1001 $this->time_start = $mtime[1] + $mtime[0]; 1002 return true; 1003 } 1004 1005 /** 1006 * Stops the debugging timer. 1007 * 1008 * @since 1.5.0 1009 * 1010 * @return int Total time spent on the query, in milliseconds 1011 */ 1012 function timer_stop() { 1013 $mtime = microtime(); 1014 $mtime = explode(' ', $mtime); 1015 $time_end = $mtime[1] + $mtime[0]; 1016 $time_total = $time_end - $this->time_start; 1017 return $time_total; 1018 } 1019 1020 /** 1021 * Wraps errors in a nice header and footer and dies. 1022 * 1023 * Will not die if wpdb::$show_errors is true 1024 * 1025 * @since 1.5.0 1026 * 1027 * @param string $message The Error message 1028 * @param string $error_code (optional) A Computer readable string to identify the error. 1029 * @return false|void 1030 */ 1031 function bail($message, $error_code = '500') { 1032 if ( !$this->show_errors ) { 1033 if ( class_exists('WP_Error') ) 1034 $this->error = new WP_Error($error_code, $message); 1035 else 1036 $this->error = $message; 1037 return false; 1038 } 1039 wp_die($message); 1040 } 1041 1042 /** 1043 * Whether or not MySQL database is at least the required minimum version. 1044 * 1045 * @since 2.5.0 1046 * @uses $wp_version 1047 * 1048 * @return WP_Error 1049 */ 1050 function check_database_version() 1051 { 1052 global $wp_version; 1053 // Make sure the server has MySQL 4.1.2 1054 if ( version_compare($this->db_version(), '4.1.2', '<') ) 1055 return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %s requires MySQL 4.1.2 or higher'), $wp_version)); 1056 } 1057 1058 /** 1059 * Whether of not the database supports collation. 1060 * 1061 * Called when WordPress is generating the table scheme. 1062 * 1063 * @since 2.5.0 1064 * 1065 * @return bool True if collation is supported, false if version does not 1066 */ 1067 function supports_collation() { 1068 return $this->has_cap( 'collation' ); 1069 } 1070 1071 /** 1072 * Generic function to determine if a database supports a particular feature 1073 * @param string $db_cap the feature 1074 * @param false|string|resource $dbh_or_table (not implemented) Which database to test. False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource. 1075 * @return bool 1076 */ 1077 function has_cap( $db_cap ) { 1078 $version = $this->db_version(); 1079 1080 switch ( strtolower( $db_cap ) ) : 1081 case 'collation' : // @since 2.5.0 1082 case 'group_concat' : // @since 2.7 1083 case 'subqueries' : // @since 2.7 1084 return version_compare($version, '4.1', '>='); 1085 break; 1086 endswitch; 1087 1088 return false; 1089 } 1090 1091 /** 1092 * Retrieve the name of the function that called wpdb. 1093 * 1094 * Requires PHP 4.3 and searches up the list of functions until it reaches 1095 * the one that would most logically had called this method. 1096 * 1097 * @since 2.5.0 1098 * 1099 * @return string The name of the calling function 1100 */ 1101 function get_caller() { 1102 // requires PHP 4.3+ 1103 if ( !is_callable('debug_backtrace') ) 1104 return ''; 1105 1106 $bt = debug_backtrace(); 1107 $caller = array(); 1108 1109 $bt = array_reverse( $bt ); 1110 foreach ( (array) $bt as $call ) { 1111 if ( @$call['class'] == __CLASS__ ) 1112 continue; 1113 $function = $call['function']; 1114 if ( isset( $call['class'] ) ) 1115 $function = $call['class'] . "->$function"; 1116 $caller[] = $function; 1117 } 1118 $caller = join( ', ', $caller ); 1119 1120 return $caller; 1121 } 1122 1123 /** 1124 * The database version number 1125 * @param false|string|resource $dbh_or_table (not implemented) Which database to test. False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource. 1126 * @return false|string false on failure, version number on success 1127 */ 1128 function db_version() { 1129 return preg_replace('/[^0-9.].*/', '', mysql_get_server_info( $this->dbh )); 1130 } 1131 } 1132 1133 if ( ! isset($wpdb) ) { 1134 /** 1135 * WordPress Database Object, if it isn't set already in wp-content/db.php 1136 * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database 1137 * @since 0.71 1138 */ 1139 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); 1140 } 1141 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Jan 8 00:19:48 2010 | Cross-referenced by PHPXref 0.7 |