Changeset 13314
- Timestamp:
- 02/22/2010 10:53:54 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/wp-db.php
r13245 r13314 13 13 * @since 0.71 14 14 */ 15 define( 'EZSQL_VERSION', 'WP1.25');15 define( 'EZSQL_VERSION', 'WP1.25' ); 16 16 17 17 /** 18 18 * @since 0.71 19 19 */ 20 define( 'OBJECT', 'OBJECT', true);20 define( 'OBJECT', 'OBJECT', true ); 21 21 22 22 /** 23 23 * @since {@internal Version Unknown}} 24 24 */ 25 define( 'OBJECT_K', 'OBJECT_K', false);25 define( 'OBJECT_K', 'OBJECT_K', false ); 26 26 27 27 /** 28 28 * @since 0.71 29 29 */ 30 define( 'ARRAY_A', 'ARRAY_A', false);30 define( 'ARRAY_A', 'ARRAY_A', false ); 31 31 32 32 /** 33 33 * @since 0.71 34 34 */ 35 define( 'ARRAY_N', 'ARRAY_N', false);35 define( 'ARRAY_N', 'ARRAY_N', false ); 36 36 37 37 /** … … 66 66 * 67 67 * @access private 68 * @since 2.5 69 * @var bool 70 */ 71 var $suppress_errors = false; 72 73 /** 74 * The last error during query. 75 * 76 * @see get_last_error() 77 * @since 2.5 78 * @access private 79 * @var string 80 */ 81 var $last_error = ''; 82 83 /** 84 * Amount of queries made 85 * 86 * @since 1.2.0 87 * @access private 88 * @var int 89 */ 90 var $num_queries = 0; 91 92 /** 93 * Count of rows returned by previous query 94 * 95 * @since 1.2 96 * @var int 97 */ 98 var $num_rows = 0; 99 100 /** 101 * Count of affected rows by previous query 102 * 103 * @since 0.71 104 * @var int 105 */ 106 var $rows_affected = 0; 107 108 /** 109 * Saved result of the last query made 110 * 111 * @since 1.2.0 112 * @access private 113 * @var array 114 */ 115 var $last_query; 116 117 /** 118 * Results of the last query made 119 * 68 120 * @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; 121 * @var mixed 122 */ 123 var $last_result; 98 124 99 125 /** … … 136 162 */ 137 163 var $ready = false; 164 165 /** 166 * {@internal Missing Description}} 167 * 168 * @since 3.0.0 169 * @var int 170 */ 138 171 var $blogid = 0; 172 173 /** 174 * {@internal Missing Description}} 175 * 176 * @since 3.0.0 177 * @var int 178 */ 139 179 var $siteid = 0; 140 180 … … 252 292 * List of deprecated WordPress tables 253 293 * 294 * @deprecated 254 295 * @since 2.9.0 255 296 * @access private … … 344 385 345 386 /** 346 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php.347 * 348 * Keys are col mn names, values are format types: 'ID' => '%d'387 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load. 388 * 389 * Keys are column names, values are format types: 'ID' => '%d' 349 390 * 350 391 * @since 2.8.0 … … 407 448 * @param string $dbhost MySQL database host 408 449 */ 409 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost) {450 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) { 410 451 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) 411 452 $this->db_connect(); 412 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost);453 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost ); 413 454 } 414 455 … … 427 468 * @param string $dbhost MySQL database host 428 469 */ 429 function __construct( $dbuser, $dbpassword, $dbname, $dbhost) {430 register_shutdown_function( array(&$this, "__destruct"));470 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { 471 register_shutdown_function( array( &$this, '__destruct' ) ); 431 472 432 473 if ( WP_DEBUG ) … … 443 484 } 444 485 445 if ( defined( 'DB_CHARSET') )486 if ( defined( 'DB_CHARSET' ) ) 446 487 $this->charset = DB_CHARSET; 447 488 448 489 $this->dbuser = $dbuser; 449 490 450 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true);451 if ( !$this->dbh) {452 $this->bail( sprintf(/*WP_I18N_DB_CONN_ERROR*/"491 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true ); 492 if ( !$this->dbh ) { 493 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 453 494 <h1>Error establishing a database connection</h1> 454 495 <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> … … 459 500 </ul> 460 501 <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> 461 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail');502 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' ); 462 503 return; 463 504 } … … 465 506 $this->ready = true; 466 507 467 if ( $this->has_cap( 'collation' ) && !empty( $this->charset) ) {468 if ( function_exists( 'mysql_set_charset') ) {469 mysql_set_charset( $this->charset, $this->dbh);508 if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) { 509 if ( function_exists( 'mysql_set_charset' ) ) { 510 mysql_set_charset( $this->charset, $this->dbh ); 470 511 $this->real_escape = true; 471 512 } else { 472 $ collation_query = "SET NAMES '{$this->charset}'";473 if ( ! empty($this->collate) )474 $ collation_query .= " COLLATE '{$this->collate}'";475 $this->query( $collation_query);513 $query = $this->prepare( 'SET NAMES %s', $this->charset ); 514 if ( ! empty( $this->collate ) ) 515 $query .= $this->prepare( ' COLLATE %s', $this->collate ); 516 $this->query( $query ); 476 517 } 477 518 } 478 519 479 $this->select( $dbname, $this->dbh);520 $this->select( $dbname, $this->dbh ); 480 521 } 481 522 … … 484 525 * 485 526 * @since 2.0.8 486 * 487 * @return bool Always true 527 * @return bool true 488 528 */ 489 529 function __destruct() { … … 504 544 function set_prefix( $prefix ) { 505 545 506 if ( preg_match( '|[^a-z0-9_]|i', $prefix) )546 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) 507 547 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); 508 548 509 if ( is_multisite() ) 510 $old_prefix = ''; 511 else 512 $old_prefix = $prefix; 549 $old_prefix = is_multisite() ? '' : $prefix; 513 550 514 551 if ( isset( $this->base_prefix ) ) … … 518 555 $this->$table = $prefix . $table; 519 556 520 if ( defined( 'VHOST') && empty( $this->blogid ) )557 if ( defined( 'VHOST' ) && empty( $this->blogid ) ) 521 558 return $old_prefix; 522 559 … … 529 566 $this->$table = $this->prefix . $table; 530 567 531 if ( defined( 'CUSTOM_USER_TABLE') )568 if ( defined( 'CUSTOM_USER_TABLE' ) ) 532 569 $this->users = CUSTOM_USER_TABLE; 533 570 534 if ( defined( 'CUSTOM_USER_META_TABLE') )571 if ( defined( 'CUSTOM_USER_META_TABLE' ) ) 535 572 $this->usermeta = CUSTOM_USER_META_TABLE; 536 573 … … 538 575 } 539 576 540 function set_blog_id($blog_id, $site_id = '') { 541 if ( !empty($site_id) ) 577 /** 578 * Sets blog id. 579 * 580 * @since 3.0.0 581 * @access public 582 * @param string $blog_id 583 * @param string $site_id. Optional. 584 * @return string previous blog id 585 */ 586 function set_blog_id( $blog_id, $site_id = '' ) { 587 if ( ! empty( $site_id ) ) 542 588 $this->siteid = $site_id; 543 589 544 $old_blog_id = $this->blogid;590 $old_blog_id = $this->blogid; 545 591 $this->blogid = $blog_id; 546 592 … … 556 602 } 557 603 558 function get_blog_prefix( $blog_id = '' ) { 604 /** 605 * Gets blog prefix. 606 * 607 * @uses is_multisite() 608 * @since 3.0.0 609 * @param int $blog_id. Optional. 610 * @return string Blog prefix. 611 */ 612 function get_blog_prefix( $blog_id = 0 ) { 559 613 if ( is_multisite() && $blog_id ) { 560 614 if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) ) … … 621 675 * @return null Always null. 622 676 */ 623 function select( $db, &$dbh) {624 if ( !@mysql_select_db($db, $dbh)) {677 function select( $db, &$dbh ) { 678 if ( !@mysql_select_db( $db, $dbh ) ) { 625 679 $this->ready = false; 626 $this->bail( sprintf(/*WP_I18N_DB_SELECT_DB*/'680 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/' 627 681 <h1>Can’t select database</h1> 628 682 <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> … … 632 686 <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> 633 687 </ul> 634 <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');688 <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' ); 635 689 return; 636 690 } 637 691 } 638 692 639 function _weak_escape($string) { 640 return addslashes($string); 641 } 642 643 function _real_escape($string) { 693 /** 694 * Weak escape 695 * 696 * @see addslashes() 697 * @since unknown 698 * @access private 699 * 700 * @param string $string 701 * @return string 702 */ 703 function _weak_escape( $string ) { 704 return addslashes( $string ); 705 } 706 707 /** 708 * Real escape 709 * 710 * @see mysql_real_escape_string() 711 * @see addslashes() 712 * @since 2.8 713 * @access private 714 * 715 * @param string $string to escape 716 * @return string escaped 717 */ 718 function _real_escape( $string ) { 644 719 if ( $this->dbh && $this->real_escape ) 645 720 return mysql_real_escape_string( $string, $this->dbh ); … … 648 723 } 649 724 650 function _escape($data) { 651 if ( is_array($data) ) { 725 /** 726 * Escape data. 727 * 728 * @see esc_sql() 729 * @since 2.8 730 * @access private 731 * 732 * @param string|array $data 733 * @return string|array escaped 734 */ 735 function _escape( $data ) { 736 if ( is_array( $data ) ) { 652 737 foreach ( (array) $data as $k => $v ) { 653 738 if ( is_array($v) ) … … 668 753 * @since 0.71 669 754 * 670 * @param string|array $data671 * @return string query safe string672 */ 673 function escape( $data) {674 if ( is_array( $data) ) {755 * @param string|array $data to escape 756 * @return string|array escaped as query safe string 757 */ 758 function escape( $data ) { 759 if ( is_array( $data ) ) { 675 760 foreach ( (array) $data as $k => $v ) { 676 if ( is_array( $v) )761 if ( is_array( $v ) ) 677 762 $data[$k] = $this->escape( $v ); 678 763 else … … 691 776 * @since 2.3.0 692 777 * 693 * @param string $s 694 */ 695 function escape_by_ref(&$string) { 778 * @param string $string to escape 779 * @return void 780 */ 781 function escape_by_ref( &$string ) { 696 782 $string = $this->_real_escape( $string ); 697 783 } … … 699 785 /** 700 786 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 787 * 788 * The following directives can be used in the query format string: 789 * %d (decimal number) 790 * %s (string) 791 * %% (literal percentage sign - no argument needed) 792 * 793 * Both %d and %s are to be left unquoted in the query string and 794 * they need an argument passed for them. Literals (%) as parts of 795 * the query must be properly written as %%. 701 796 * 702 797 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). … … 716 811 * 717 812 * @param string $query Query statement with sprintf()-like placeholders 718 * @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()}. 719 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. 720 * @return null|string Sanitized query string 721 */ 722 function prepare($query = null) { // ( $query, *$args ) 813 * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like 814 * {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if 815 * being called like {@link http://php.net/sprintf sprintf()}. 816 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like 817 * {@link http://php.net/sprintf sprintf()}. 818 * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string 819 * if there was something to prepare 820 */ 821 function prepare( $query = null ) { // ( $query, *$args ) 723 822 if ( is_null( $query ) ) 724 823 return; 725 824 $args = func_get_args(); 726 array_shift( $args);825 array_shift( $args ); 727 826 // If args were passed as an array (as in vsprintf), move them up 728 if ( isset( $args[0]) && is_array($args[0]) )827 if ( isset( $args[0] ) && is_array($args[0]) ) 729 828 $args = $args[0]; 730 $query = str_replace( "'%s'", '%s', $query); // in case someone mistakenly already singlequoted it731 $query = str_replace( '"%s"', '%s', $query); // doublequote unquoting732 $query = str_replace( '%s', "'%s'", $query); // quote the strings733 array_walk( $args, array(&$this, 'escape_by_ref'));734 return @vsprintf( $query, $args);829 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it 830 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting 831 $query = str_replace( '%s', "'%s'", $query ); // quote the strings 832 array_walk( $args, array( &$this, 'escape_by_ref' ) ); 833 return @vsprintf( $query, $args ); 735 834 } 736 835 … … 744 843 * @return bool False if the showing of errors is disabled. 745 844 */ 746 function print_error( $str = '') {845 function print_error( $str = '' ) { 747 846 global $EZSQL_ERROR; 748 847 749 if (!$str) $str = mysql_error($this->dbh); 750 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str); 848 if ( !$str ) 849 $str = mysql_error( $this->dbh ); 850 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 751 851 752 852 if ( $this->suppress_errors ) … … 781 881 die( $msg ); 782 882 } else { 783 $str = htmlspecialchars($str, ENT_QUOTES);784 $query = htmlspecialchars( $this->last_query, ENT_QUOTES);883 $str = htmlspecialchars( $str, ENT_QUOTES ); 884 $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); 785 885 786 886 print "<div id='error'> … … 800 900 * 801 901 * @since 0.71 902 * @see wpdb::hide_errors() 802 903 * 803 904 * @param bool $show Whether to show or hide errors … … 813 914 * Disables showing of database errors. 814 915 * 815 * @since 0.71 916 * By default database errors are not shown. 917 * 918 * @since 0.71 919 * @see wpdb::show_errors() 816 920 * 817 921 * @return bool Whether showing of errors was active or not … … 826 930 * Whether to suppress database errors. 827 931 * 828 * @param unknown_type $suppress 829 * @return unknown 932 * By default database errors are suppressed, with a simple 933 * call to this function they can be enabled. 934 * 935 * @since 2.5 936 * @see wpdb::hide_errors() 937 * @param bool $suppress Optional. New value. Defaults to true. 938 * @return bool Old value 830 939 */ 831 940 function suppress_errors( $suppress = true ) { 832 941 $errors = $this->suppress_errors; 833 $this->suppress_errors = $suppress;942 $this->suppress_errors = (bool) $suppress; 834 943 return $errors; 835 944 } … … 839 948 * 840 949 * @since 0.71 950 * @return void 841 951 */ 842 952 function flush() { 843 953 $this->last_result = array(); 844 $this->col_info = null;845 $this->last_query = null;954 $this->col_info = null; 955 $this->last_query = null; 846 956 } 847 957 848 958 function db_connect( $query = "SELECT" ) { 849 959 global $db_list, $global_db_list; 850 if ( is_array( $db_list ) == false)960 if ( ! is_array( $db_list ) ) 851 961 return true; 852 962 … … 868 978 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 869 979 if (!$this->$dbhname ) { 870 $this->bail( "980 $this->bail(/*WP_I18N_DB_CONNECT_DB*/" 871 981 <h1>Error establishing a database connection</h1> 872 982 <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>{$details['db_host']}</code>. This could mean your host's database server is down.</p> … … 877 987 </ul> 878 988 <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> 879 " );989 "/*/WP_I18N_DB_CONNECT_DB*/); 880 990 } 881 991 $this->select( $details[ 'db_name' ], $this->$dbhname ); … … 889 999 * @since 0.71 890 1000 * 891 * @param string $query 1001 * @param string $query Database query 892 1002 * @return int|false Number of rows affected/selected or false on error 893 1003 */ 894 function query( $query) {1004 function query( $query ) { 895 1005 if ( ! $this->ready ) 896 1006 return false; … … 898 1008 // filter the query, if filters are available 899 1009 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method 900 if ( function_exists( 'apply_filters') )901 $query = apply_filters( 'query', $query);902 903 // initiali se return1010 if ( function_exists( 'apply_filters' ) ) 1011 $query = apply_filters( 'query', $query ); 1012 1013 // initialize return 904 1014 $return_val = 0; 905 1015 $this->flush(); … … 911 1021 $this->last_query = $query; 912 1022 913 // Perform the query via std mysql_query function.. 914 if ( defined('SAVEQUERIES') && SAVEQUERIES ) 1023 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 915 1024 $this->timer_start(); 916 1025 … … 940 1049 } 941 1050 942 $this->result = @mysql_query( $query, $dbh);1051 $this->result = @mysql_query( $query, $dbh ); 943 1052 ++$this->num_queries; 944 1053 … … 947 1056 948 1057 // If there is an error then take note of it.. 949 if ( $this->last_error = mysql_error( $dbh) ) {1058 if ( $this->last_error = mysql_error( $dbh ) ) { 950 1059 $this->print_error(); 951 1060 return false; 952 1061 } 953 1062 954 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {955 $this->rows_affected = mysql_affected_rows( $dbh);1063 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1064 $this->rows_affected = mysql_affected_rows( $dbh ); 956 1065 // Take note of the insert_id 957 if ( preg_match( "/^\\s*(insert|replace) /i",$query) ) {1066 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 958 1067 $this->insert_id = mysql_insert_id($dbh); 959 1068 } … … 962 1071 } else { 963 1072 $i = 0; 964 while ( $i < @mysql_num_fields($this->result)) {965 $this->col_info[$i] = @mysql_fetch_field( $this->result);1073 while ( $i < @mysql_num_fields( $this->result ) ) { 1074 $this->col_info[$i] = @mysql_fetch_field( $this->result ); 966 1075 $i++; 967 1076 } 968 1077 $num_rows = 0; 969 while ( $row = @mysql_fetch_object( $this->result) ) {1078 while ( $row = @mysql_fetch_object( $this->result ) ) { 970 1079 $this->last_result[$num_rows] = $row; 971 1080 $num_rows++; 972 1081 } 973 1082 974 @mysql_free_result( $this->result);1083 @mysql_free_result( $this->result ); 975 1084 976 1085 // Log number of rows the query returned 1086 // and return number of rows selected 977 1087 $this->num_rows = $num_rows; 978 979 // Return number of rows selected 980 $return_val = $this->num_rows; 1088 $return_val = $num_rows; 981 1089 } 982 1090 … … 999 1107 * @return int|false The number of rows inserted, or false on error. 1000 1108 */ 1001 function insert( $table, $data, $format = null) {1109 function insert( $table, $data, $format = null ) { 1002 1110 $formats = $format = (array) $format; 1003 $fields = array_keys( $data);1111 $fields = array_keys( $data ); 1004 1112 $formatted_fields = array(); 1005 1113 foreach ( $fields as $field ) { 1006 if ( !empty( $format) )1007 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1008 elseif ( isset( $this->field_types[$field]) )1114 if ( !empty( $format ) ) 1115 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1116 elseif ( isset( $this->field_types[$field] ) ) 1009 1117 $form = $this->field_types[$field]; 1010 1118 else … … 1013 1121 } 1014 1122 $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; 1015 return $this->query( $this->prepare( $sql, $data ) );1123 return $this->query( $this->prepare( $sql, $data ) ); 1016 1124 } 1017 1125 … … 1034 1142 * @return int|false The number of rows updated, or false on error. 1035 1143 */ 1036 function update( $table, $data, $where, $format = null, $where_format = null) {1037 if ( ! is_array( $where ) )1144 function update( $table, $data, $where, $format = null, $where_format = null ) { 1145 if ( ! is_array( $where ) ) 1038 1146 return false; 1039 1147 1040 1148 $formats = $format = (array) $format; 1041 1149 $bits = $wheres = array(); 1042 foreach ( (array) array_keys( $data) as $field ) {1043 if ( !empty( $format) )1044 $form = ( $form = array_shift( $formats) ) ? $form : $format[0];1150 foreach ( (array) array_keys( $data ) as $field ) { 1151 if ( !empty( $format ) ) 1152 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1045 1153 elseif ( isset($this->field_types[$field]) ) 1046 1154 $form = $this->field_types[$field]; … … 1051 1159 1052 1160 $where_formats = $where_format = (array) $where_format; 1053 foreach ( (array) array_keys( $where) as $field ) {1054 if ( !empty( $where_format) )1055 $form = ( $form = array_shift( $where_formats) ) ? $form : $where_format[0];1056 elseif ( isset( $this->field_types[$field]) )1161 foreach ( (array) array_keys( $where ) as $field ) { 1162 if ( !empty( $where_format ) ) 1163 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1164 elseif ( isset( $this->field_types[$field] ) ) 1057 1165 $form = $this->field_types[$field]; 1058 1166 else … … 1062 1170 1063 1171 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); 1064 return $this->query( $this->prepare( $sql, array_merge( array_values($data), array_values($where))) );1172 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 1065 1173 } 1066 1174 … … 1074 1182 * @since 0.71 1075 1183 * 1076 * @param string|null $query SQL query. If null, usethe result from the previous query.1184 * @param string|null $query Optional. SQL query. If null (default), uses the result from the previous query. 1077 1185 * @param int $x (optional) Column of value to return. Indexed from 0. 1078 1186 * @param int $y (optional) Row of value to return. Indexed from 0. 1079 * @return string Database query result1080 */ 1081 function get_var( $query=null, $x = 0, $y = 0) {1082 $this->func_call = "\$db->get_var(\"$query\", $x,$y)";1187 * @return string|null Database query result, or null on failure 1188 */ 1189 function get_var( $query = null, $x = 0, $y = 0 ) { 1190 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; 1083 1191 if ( $query ) 1084 $this->query( $query);1192 $this->query( $query ); 1085 1193 1086 1194 // Extract var out of cached results based x,y vals 1087 1195 if ( !empty( $this->last_result[$y] ) ) { 1088 $values = array_values( get_object_vars($this->last_result[$y]));1196 $values = array_values( get_object_vars( $this->last_result[$y] ) ); 1089 1197 } 1090 1198 1091 1199 // If there is a value return it else return null 1092 return ( isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;1200 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; 1093 1201 } 1094 1202 … … 1103 1211 * @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. 1104 1212 * @param int $y (optional) Row to return. Indexed from 0. 1105 * @return mixed Database query result in format specifed by $output 1106 */ 1107 function get_row( $query = null, $output = OBJECT, $y = 0) {1213 * @return mixed Database query result in format specifed by $output or null on failure 1214 */ 1215 function get_row( $query = null, $output = OBJECT, $y = 0 ) { 1108 1216 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 1109 1217 if ( $query ) 1110 $this->query( $query);1218 $this->query( $query ); 1111 1219 else 1112 1220 return null; 1113 1221 1114 if ( !isset( $this->last_result[$y]) )1222 if ( !isset( $this->last_result[$y] ) ) 1115 1223 return null; 1116 1224 … … 1118 1226 return $this->last_result[$y] ? $this->last_result[$y] : null; 1119 1227 } elseif ( $output == ARRAY_A ) { 1120 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y]) : null;1228 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; 1121 1229 } elseif ( $output == ARRAY_N ) { 1122 return $this->last_result[$y] ? array_values( get_object_vars($this->last_result[$y])) : null;1230 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; 1123 1231 } else { 1124 1232 $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*/); … … 1135 1243 * @since 0.71 1136 1244 * 1137 * @param string|null $query SQL query. If null, use the result from the previous query.1138 * @param int $x Column to return.Indexed from 0.1139 * @return array Database query result. 1140 */ 1141 function get_col( $query = null , $x = 0) {1245 * @param string|null $query Optional. SQL query. If null (default), use the result from the previous query. 1246 * @param int $x Optional. Column to return. Indexed from 0. 1247 * @return array Database query result. Array indexed from 0 by SQL result row number. 1248 */ 1249 function get_col( $query = null , $x = 0 ) { 1142 1250 if ( $query ) 1143 $this->query( $query);1251 $this->query( $query ); 1144 1252 1145 1253 $new_array = array(); 1146 1254 // Extract the column values 1147 for ( $i=0; $i < count( $this->last_result); $i++ ) {1148 $new_array[$i] = $this->get_var( null, $x, $i);1255 for ( $i=0; $i < count( $this->last_result ); $i++ ) { 1256 $new_array[$i] = $this->get_var( null, $x, $i ); 1149 1257 } 1150 1258 return $new_array; … … 1162 1270 * @return mixed Database query results 1163 1271 */ 1164 function get_results( $query = null, $output = OBJECT) {1272 function get_results( $query = null, $output = OBJECT ) { 1165 1273 $this->func_call = "\$db->get_results(\"$query\", $output)"; 1166 1274 1167 1275 if ( $query ) 1168 $this->query( $query);1276 $this->query( $query ); 1169 1277 else 1170 1278 return null; … … 1207 1315 * @since 0.71 1208 1316 * 1209 * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill1210 * @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 type1317 * @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 1318 * @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 1211 1319 * @return mixed Column Results 1212 1320 */ 1213 function get_col_info( $info_type = 'name', $col_offset = -1) {1321 function get_col_info( $info_type = 'name', $col_offset = -1 ) { 1214 1322 if ( $this->col_info ) { 1215 1323 if ( $col_offset == -1 ) { 1216 $i = 0;1324 $i = 0; 1217 1325 $new_array = array(); 1218 1326 foreach( (array) $this->col_info as $col ) { … … 1235 1343 */ 1236 1344 function timer_start() { 1237 $mtime = microtime(); 1238 $mtime = explode(' ', $mtime); 1345 $mtime = explode( ' ', microtime() ); 1239 1346 $this->time_start = $mtime[1] + $mtime[0]; 1240 1347 return true; … … 1249 1356 */ 1250 1357 function timer_stop() { 1251 $mtime = microtime(); 1252 $mtime = explode(' ', $mtime); 1253 $time_end = $mtime[1] + $mtime[0]; 1358 $mtime = explode( ' ', microtime() ); 1359 $time_end = $mtime[1] + $mtime[0]; 1254 1360 $time_total = $time_end - $this->time_start; 1255 1361 return $time_total; … … 1267 1373 * @return false|void 1268 1374 */ 1269 function bail( $message, $error_code = '500') {1375 function bail( $message, $error_code = '500' ) { 1270 1376 if ( !$this->show_errors ) { 1271 if ( class_exists( 'WP_Error') )1377 if ( class_exists( 'WP_Error' ) ) 1272 1378 $this->error = new WP_Error($error_code, $message); 1273 1379 else … … 1283 1389 * @since 2.5.0 1284 1390 * @uses $wp_version 1391 * @uses $required_mysql_version 1285 1392 * 1286 1393 * @return WP_Error … … 1290 1397 // Make sure the server has the required MySQL version 1291 1398 if ( version_compare($this->db_version(), $required_mysql_version, '<') ) 1292 return new WP_Error('database_version', sprintf(__('<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher'), $wp_version, $required_mysql_version));1399 return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version )); 1293 1400 } 1294 1401 … … 1307 1414 1308 1415 /** 1309 * Generic function to determine if a database supports a particular feature 1416 * Determine if a database supports a particular feature 1417 * 1418 * @since 2.7 1419 * @see wpdb::db_version() 1420 * 1310 1421 * @param string $db_cap the feature 1311 * @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. 1422 * @param false|string|resource $dbh_or_table. Not implemented. 1423 * 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. 1312 1424 * @return bool 1313 1425 */ … … 1315 1427 $version = $this->db_version(); 1316 1428 1317 switch ( strtolower( $db_cap ) ) : 1318 case 'collation' : // @since 2.5.0 1319 case 'group_concat' : // @since 2.7 1320 case 'subqueries' : // @since 2.7 1321 return version_compare($version, '4.1', '>='); 1322 break; 1323 endswitch; 1429 switch ( strtolower( $db_cap ) ) { 1430 case 'collation' : // @since 2.5.0 1431 case 'group_concat' : // @since 2.7 1432 case 'subqueries' : // @since 2.7 1433 return version_compare( $version, '4.1', '>=' ); 1434 }; 1324 1435 1325 1436 return false; … … 1356 1467 /** 1357 1468 * The database version number 1358 * @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. 1469 * @param false|string|resource $dbh_or_table. Not implemented. 1470 * 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. 1359 1471 * @return false|string false on failure, version number on success 1360 1472 */ 1361 1473 function db_version() { 1362 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));1474 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1363 1475 } 1364 1476 }
Note: See TracChangeset
for help on using the changeset viewer.