Changes from branches/3.0/wp-includes/wp-db.php at r15470 to trunk/wp-includes/wp-db.php at r17079
- File:
-
- 1 edited
-
trunk/wp-includes/wp-db.php (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/wp-db.php
r15470 r17079 49 49 * @subpackage Database 50 50 * @since 0.71 51 * @final52 51 */ 53 52 class wpdb { … … 66 65 * 67 66 * @access private 68 * @since 2.5 67 * @since 2.5.0 69 68 * @var bool 70 69 */ … … 75 74 * 76 75 * @see get_last_error() 77 * @since 2.5 76 * @since 2.5.0 78 77 * @access private 79 78 * @var string … … 93 92 * Count of rows returned by previous query 94 93 * 95 * @since 1.2 94 * @since 1.2.0 96 95 * @access private 97 96 * @var int … … 457 456 * A textual description of the last query/get_row/get_var call 458 457 * 459 * @since unknown458 * @since 3.0.0 460 459 * @access public 461 460 * @var string … … 477 476 */ 478 477 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) { 479 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )480 $this->db_connect();481 478 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost ); 482 479 } … … 503 500 $this->show_errors(); 504 501 505 if ( is_multisite() ) { 502 $this->init_charset(); 503 504 $this->dbuser = $dbuser; 505 $this->dbpassword = $dbpassword; 506 $this->dbname = $dbname; 507 $this->dbhost = $dbhost; 508 509 $this->db_connect(); 510 } 511 512 /** 513 * PHP5 style destructor and will run when database object is destroyed. 514 * 515 * @see wpdb::__construct() 516 * @since 2.0.8 517 * @return bool true 518 */ 519 function __destruct() { 520 return true; 521 } 522 523 /** 524 * Set $this->charset and $this->collate 525 * 526 * @since 3.1.0 527 */ 528 function init_charset() { 529 if ( function_exists('is_multisite') && is_multisite() ) { 506 530 $this->charset = 'utf8'; 507 531 if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) … … 515 539 if ( defined( 'DB_CHARSET' ) ) 516 540 $this->charset = DB_CHARSET; 517 518 $this->dbuser = $dbuser; 519 520 $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true ); 521 if ( !$this->dbh ) { 522 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 523 <h1>Error establishing a database connection</h1> 524 <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p> 525 <ul> 526 <li>Are you sure you have the correct username and password?</li> 527 <li>Are you sure that you have typed the correct hostname?</li> 528 <li>Are you sure that the database server is running?</li> 529 </ul> 530 <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p> 531 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' ); 532 return; 533 } 534 535 $this->ready = true; 536 537 if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) { 538 if ( function_exists( 'mysql_set_charset' ) ) { 539 mysql_set_charset( $this->charset, $this->dbh ); 541 } 542 543 /** 544 * Sets the connection's character set. 545 * 546 * @since 3.1.0 547 * 548 * @param resource $dbh The resource given by mysql_connect 549 * @param string $charset The character set (optional) 550 * @param string $collate The collation (optional) 551 */ 552 function set_charset($dbh, $charset = null, $collate = null) { 553 if ( !isset($charset) ) 554 $charset = $this->charset; 555 if ( !isset($collate) ) 556 $collate = $this->collate; 557 if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) { 558 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) { 559 mysql_set_charset( $charset, $dbh ); 540 560 $this->real_escape = true; 541 561 } else { 542 $query = $this->prepare( 'SET NAMES %s', $ this->charset );543 if ( ! empty( $ this->collate ) )544 $query .= $this->prepare( ' COLLATE %s', $ this->collate );545 $this->query( $query);562 $query = $this->prepare( 'SET NAMES %s', $charset ); 563 if ( ! empty( $collate ) ) 564 $query .= $this->prepare( ' COLLATE %s', $collate ); 565 mysql_query( $query, $dbh ); 546 566 } 547 567 } 548 549 $this->select( $dbname, $this->dbh );550 }551 552 /**553 * PHP5 style destructor and will run when database object is destroyed.554 *555 * @see wpdb::__construct()556 * @since 2.0.8557 * @return bool true558 */559 function __destruct() {560 return true;561 568 } 562 569 … … 738 745 */ 739 746 function select( $db, $dbh = null) { 740 if ( is_null($dbh) ) 747 if ( is_null($dbh) ) 741 748 $dbh = $this->dbh; 742 749 743 750 if ( !@mysql_select_db( $db, $dbh ) ) { 744 751 $this->ready = false; 745 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/' 746 <h1>Can’t select database</h1> 752 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'<h1>Can’t select database</h1> 747 753 <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p> 748 754 <ul> … … 775 781 * @see mysql_real_escape_string() 776 782 * @see addslashes() 777 * @since 2.8 783 * @since 2.8.0 778 784 * @access private 779 785 * … … 791 797 * Escape data. Works on arrays. 792 798 * 793 * @uses wpdb::_escape()794 * @uses wpdb::_real_escape()795 * @since 2.8 799 * @uses wpdb::_escape() 800 * @uses wpdb::_real_escape() 801 * @since 2.8.0 796 802 * @access private 797 803 * … … 996 1002 * call to this function they can be enabled. 997 1003 * 998 * @since 2.5 1004 * @since 2.5.0 999 1005 * @see wpdb::hide_errors() 1000 1006 * @param bool $suppress Optional. New value. Defaults to true. … … 1019 1025 } 1020 1026 1021 function db_connect( $query = "SELECT" ) { 1027 /** 1028 * Connect to and select database 1029 * 1030 * @since 3.0.0 1031 */ 1032 function db_connect() { 1022 1033 global $db_list, $global_db_list; 1023 if ( ! is_array( $db_list ) ) 1024 return true; 1025 1026 if ( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) { 1027 $action = 'global'; 1028 $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ]; 1029 $this->db_global = $details; 1030 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) { 1031 $action = 'write'; 1032 $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ]; 1033 $this->db_write = $details; 1034 1035 if ( WP_DEBUG ) { 1036 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 1034 1037 } else { 1035 $action = ''; 1036 $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ]; 1037 $this->db_read = $details; 1038 } 1039 1040 $dbhname = "dbh" . $action; 1041 $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); 1042 if (!$this->$dbhname ) { 1038 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 1039 } 1040 1041 if ( !$this->dbh ) { 1043 1042 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 1044 1043 <h1>Error establishing a database connection</h1> … … 1050 1049 </ul> 1051 1050 <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p> 1052 "/*/WP_I18N_DB_CONN_ERROR*/, $details['db_host'] ), 'db_connect_fail' ); 1053 } 1054 $this->select( $details[ 'db_name' ], $this->$dbhname ); 1051 "/*/WP_I18N_DB_CONN_ERROR*/, $this->dbhost ), 'db_connect_fail' ); 1052 1053 // If show errors is disabled then we need to die anyway as we don't have a working DB connection 1054 // unless we're trying to test the initial connection, in which case setup-config.php will handle. 1055 if ( defined( 'WP_SETUP_CONFIG' ) ) 1056 return; 1057 1058 die(); 1059 } 1060 1061 $this->set_charset( $this->dbh ); 1062 1063 $this->ready = true; 1064 1065 $this->select( $this->dbname, $this->dbh ); 1055 1066 } 1056 1067 … … 1085 1096 $this->timer_start(); 1086 1097 1087 // use $this->dbh for read ops, and $this->dbhwrite for write ops 1088 // use $this->dbhglobal for gloal table ops 1089 unset( $dbh ); 1090 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) { 1091 if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) { 1092 if( false == isset( $this->dbhglobal ) ) { 1093 $this->db_connect( $query ); 1094 } 1095 $dbh =& $this->dbhglobal; 1096 $this->last_db_used = "global"; 1097 } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) { 1098 if( false == isset( $this->dbhwrite ) ) { 1099 $this->db_connect( $query ); 1100 } 1101 $dbh =& $this->dbhwrite; 1102 $this->last_db_used = "write"; 1103 } else { 1104 $dbh =& $this->dbh; 1105 $this->last_db_used = "read"; 1106 } 1107 } else { 1108 $dbh =& $this->dbh; 1109 $this->last_db_used = "other/read"; 1110 } 1111 1112 $this->result = @mysql_query( $query, $dbh ); 1098 $this->result = @mysql_query( $query, $this->dbh ); 1113 1099 $this->num_queries++; 1114 1100 … … 1117 1103 1118 1104 // If there is an error then take note of it.. 1119 if ( $this->last_error = mysql_error( $ dbh ) ) {1105 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1120 1106 $this->print_error(); 1121 1107 return false; … … 1123 1109 1124 1110 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1125 $this->rows_affected = mysql_affected_rows( $ dbh );1111 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1126 1112 // Take note of the insert_id 1127 1113 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 1128 $this->insert_id = mysql_insert_id($ dbh);1114 $this->insert_id = mysql_insert_id($this->dbh); 1129 1115 } 1130 1116 // Return number of rows affected … … 1532 1518 * Determine if a database supports a particular feature 1533 1519 * 1534 * @since 2.7 1520 * @since 2.7.0 1535 1521 * @see wpdb::db_version() 1536 1522 * … … 1546 1532 case 'subqueries' : // @since 2.7 1547 1533 return version_compare( $version, '4.1', '>=' ); 1534 case 'set_charset' : 1535 return version_compare($version, '5.0.7', '>='); 1548 1536 }; 1549 1537 … … 1577 1565 * The database version number. 1578 1566 * 1567 * @since 2.7.0 1568 * 1579 1569 * @return false|string false on failure, version number on success 1580 1570 */ … … 1584 1574 } 1585 1575 1586 if ( ! isset( $wpdb ) ) {1587 /**1588 * WordPress Database Object, if it isn't set already in wp-content/db.php1589 * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database1590 * @since 0.711591 */1592 $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );1593 }1594 1576 ?>
Note: See TracChangeset
for help on using the changeset viewer.