Changes from trunk/wp-includes/wp-db.php at r17079 to branches/3.0/wp-includes/wp-db.php at r15470
- File:
-
- 1 edited
-
branches/3.0/wp-includes/wp-db.php (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/3.0/wp-includes/wp-db.php
r17079 r15470 49 49 * @subpackage Database 50 50 * @since 0.71 51 * @final 51 52 */ 52 53 class wpdb { … … 65 66 * 66 67 * @access private 67 * @since 2.5 .068 * @since 2.5 68 69 * @var bool 69 70 */ … … 74 75 * 75 76 * @see get_last_error() 76 * @since 2.5 .077 * @since 2.5 77 78 * @access private 78 79 * @var string … … 92 93 * Count of rows returned by previous query 93 94 * 94 * @since 1.2 .095 * @since 1.2 95 96 * @access private 96 97 * @var int … … 456 457 * A textual description of the last query/get_row/get_var call 457 458 * 458 * @since 3.0.0459 * @since unknown 459 460 * @access public 460 461 * @var string … … 476 477 */ 477 478 function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) { 479 if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) 480 $this->db_connect(); 478 481 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost ); 479 482 } … … 500 503 $this->show_errors(); 501 504 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() ) { 505 if ( is_multisite() ) { 530 506 $this->charset = 'utf8'; 531 507 if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) … … 539 515 if ( defined( 'DB_CHARSET' ) ) 540 516 $this->charset = DB_CHARSET; 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 ); 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 ); 560 540 $this->real_escape = true; 561 541 } else { 562 $query = $this->prepare( 'SET NAMES %s', $ charset );563 if ( ! empty( $ collate ) )564 $query .= $this->prepare( ' COLLATE %s', $ collate );565 mysql_query( $query, $dbh);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 ); 566 546 } 567 547 } 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.8 557 * @return bool true 558 */ 559 function __destruct() { 560 return true; 568 561 } 569 562 … … 745 738 */ 746 739 function select( $db, $dbh = null) { 747 if ( is_null($dbh) ) 740 if ( is_null($dbh) ) 748 741 $dbh = $this->dbh; 749 742 750 743 if ( !@mysql_select_db( $db, $dbh ) ) { 751 744 $this->ready = false; 752 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'<h1>Can’t select database</h1> 745 $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/' 746 <h1>Can’t select database</h1> 753 747 <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> 754 748 <ul> … … 781 775 * @see mysql_real_escape_string() 782 776 * @see addslashes() 783 * @since 2.8 .0777 * @since 2.8 784 778 * @access private 785 779 * … … 797 791 * Escape data. Works on arrays. 798 792 * 799 * @uses wpdb::_escape()800 * @uses wpdb::_real_escape()801 * @since 2.8 .0793 * @uses wpdb::_escape() 794 * @uses wpdb::_real_escape() 795 * @since 2.8 802 796 * @access private 803 797 * … … 1002 996 * call to this function they can be enabled. 1003 997 * 1004 * @since 2.5 .0998 * @since 2.5 1005 999 * @see wpdb::hide_errors() 1006 1000 * @param bool $suppress Optional. New value. Defaults to true. … … 1025 1019 } 1026 1020 1027 /** 1028 * Connect to and select database 1029 * 1030 * @since 3.0.0 1031 */ 1032 function db_connect() { 1021 function db_connect( $query = "SELECT" ) { 1033 1022 global $db_list, $global_db_list; 1034 1035 if ( WP_DEBUG ) { 1036 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 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; 1037 1034 } else { 1038 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 1039 } 1040 1041 if ( !$this->dbh ) { 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 ) { 1042 1043 $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/" 1043 1044 <h1>Error establishing a database connection</h1> … … 1049 1050 </ul> 1050 1051 <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> 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 ); 1052 "/*/WP_I18N_DB_CONN_ERROR*/, $details['db_host'] ), 'db_connect_fail' ); 1053 } 1054 $this->select( $details[ 'db_name' ], $this->$dbhname ); 1066 1055 } 1067 1056 … … 1096 1085 $this->timer_start(); 1097 1086 1098 $this->result = @mysql_query( $query, $this->dbh ); 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 ); 1099 1113 $this->num_queries++; 1100 1114 … … 1103 1117 1104 1118 // If there is an error then take note of it.. 1105 if ( $this->last_error = mysql_error( $ this->dbh ) ) {1119 if ( $this->last_error = mysql_error( $dbh ) ) { 1106 1120 $this->print_error(); 1107 1121 return false; … … 1109 1123 1110 1124 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) { 1111 $this->rows_affected = mysql_affected_rows( $ this->dbh );1125 $this->rows_affected = mysql_affected_rows( $dbh ); 1112 1126 // Take note of the insert_id 1113 1127 if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) { 1114 $this->insert_id = mysql_insert_id($ this->dbh);1128 $this->insert_id = mysql_insert_id($dbh); 1115 1129 } 1116 1130 // Return number of rows affected … … 1518 1532 * Determine if a database supports a particular feature 1519 1533 * 1520 * @since 2.7 .01534 * @since 2.7 1521 1535 * @see wpdb::db_version() 1522 1536 * … … 1532 1546 case 'subqueries' : // @since 2.7 1533 1547 return version_compare( $version, '4.1', '>=' ); 1534 case 'set_charset' :1535 return version_compare($version, '5.0.7', '>=');1536 1548 }; 1537 1549 … … 1565 1577 * The database version number. 1566 1578 * 1567 * @since 2.7.01568 *1569 1579 * @return false|string false on failure, version number on success 1570 1580 */ … … 1574 1584 } 1575 1585 1586 if ( ! isset( $wpdb ) ) { 1587 /** 1588 * WordPress Database Object, if it isn't set already in wp-content/db.php 1589 * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database 1590 * @since 0.71 1591 */ 1592 $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); 1593 } 1576 1594 ?>
Note: See TracChangeset
for help on using the changeset viewer.