web/wp-includes/wp-db.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
    86 	var $num_queries = 0;
    86 	var $num_queries = 0;
    87 
    87 
    88 	/**
    88 	/**
    89 	 * Count of rows returned by previous query
    89 	 * Count of rows returned by previous query
    90 	 *
    90 	 *
    91 	 * @since 1.2.0
    91 	 * @since 0.71
    92 	 * @access private
    92 	 * @access private
    93 	 * @var int
    93 	 * @var int
    94 	 */
    94 	 */
    95 	var $num_rows = 0;
    95 	var $num_rows = 0;
    96 
    96 
   111 	 * @var int
   111 	 * @var int
   112 	 */
   112 	 */
   113 	var $insert_id = 0;
   113 	var $insert_id = 0;
   114 
   114 
   115 	/**
   115 	/**
   116 	 * Saved result of the last query made
   116 	 * Last query made
   117 	 *
   117 	 *
   118 	 * @since 1.2.0
   118 	 * @since 0.71
   119 	 * @access private
   119 	 * @access private
   120 	 * @var array
   120 	 * @var array
   121 	 */
   121 	 */
   122 	var $last_query;
   122 	var $last_query;
   123 
   123 
   124 	/**
   124 	/**
   125 	 * Results of the last query made
   125 	 * Results of the last query made
   126 	 *
   126 	 *
   127 	 * @since 1.0.0
   127 	 * @since 0.71
   128 	 * @access private
   128 	 * @access private
   129 	 * @var array|null
   129 	 * @var array|null
   130 	 */
   130 	 */
   131 	var $last_result;
   131 	var $last_result;
   132 
   132 
   133 	/**
   133 	/**
       
   134 	 * MySQL result, which is either a resource or boolean.
       
   135 	 *
       
   136 	 * @since 0.71
       
   137 	 * @access protected
       
   138 	 * @var mixed
       
   139 	 */
       
   140 	protected $result;
       
   141 
       
   142 	/**
   134 	 * Saved info on the table column
   143 	 * Saved info on the table column
   135 	 *
   144 	 *
   136 	 * @since 1.2.0
   145 	 * @since 0.71
   137 	 * @access private
   146 	 * @access protected
   138 	 * @var array
   147 	 * @var array
   139 	 */
   148 	 */
   140 	var $col_info;
   149 	protected $col_info;
   141 
   150 
   142 	/**
   151 	/**
   143 	 * Saved queries that were executed
   152 	 * Saved queries that were executed
   144 	 *
   153 	 *
   145 	 * @since 1.5.0
   154 	 * @since 1.5.0
   153 	 *
   162 	 *
   154 	 * You can set this to have multiple WordPress installations
   163 	 * You can set this to have multiple WordPress installations
   155 	 * in a single database. The second reason is for possible
   164 	 * in a single database. The second reason is for possible
   156 	 * security precautions.
   165 	 * security precautions.
   157 	 *
   166 	 *
   158 	 * @since 0.71
   167 	 * @since 2.5.0
   159 	 * @access private
   168 	 * @access private
   160 	 * @var string
   169 	 * @var string
   161 	 */
   170 	 */
   162 	var $prefix = '';
   171 	var $prefix = '';
   163 
   172 
   164 	/**
   173 	/**
   165 	 * Whether the database queries are ready to start executing.
   174 	 * Whether the database queries are ready to start executing.
   166 	 *
   175 	 *
   167 	 * @since 2.5.0
   176 	 * @since 2.3.2
   168 	 * @access private
   177 	 * @access private
   169 	 * @var bool
   178 	 * @var bool
   170 	 */
   179 	 */
   171 	var $ready = false;
   180 	var $ready = false;
   172 
   181 
   443 
   452 
   444 	/**
   453 	/**
   445 	 * Database Username
   454 	 * Database Username
   446 	 *
   455 	 *
   447 	 * @since 2.9.0
   456 	 * @since 2.9.0
   448 	 * @access private
   457 	 * @access protected
   449 	 * @var string
   458 	 * @var string
   450 	 */
   459 	 */
   451 	var $dbuser;
   460 	protected $dbuser;
       
   461 
       
   462 	/**
       
   463 	 * Database Password
       
   464 	 *
       
   465 	 * @since 3.1.0
       
   466 	 * @access protected
       
   467 	 * @var string
       
   468 	 */
       
   469 	protected $dbpassword;
       
   470 
       
   471 	/**
       
   472 	 * Database Name
       
   473 	 *
       
   474 	 * @since 3.1.0
       
   475 	 * @access protected
       
   476 	 * @var string
       
   477 	 */
       
   478 	protected $dbname;
       
   479 
       
   480 	/**
       
   481 	 * Database Host
       
   482 	 *
       
   483 	 * @since 3.1.0
       
   484 	 * @access protected
       
   485 	 * @var string
       
   486 	 */
       
   487 	protected $dbhost;
       
   488 
       
   489 	/**
       
   490 	 * Database Handle
       
   491 	 *
       
   492 	 * @since 0.71
       
   493 	 * @access protected
       
   494 	 * @var string
       
   495 	 */
       
   496 	protected $dbh;
   452 
   497 
   453 	/**
   498 	/**
   454 	 * A textual description of the last query/get_row/get_var call
   499 	 * A textual description of the last query/get_row/get_var call
   455 	 *
   500 	 *
   456 	 * @since 3.0.0
   501 	 * @since 3.0.0
   487 	 * @param string $dbpassword MySQL database password
   532 	 * @param string $dbpassword MySQL database password
   488 	 * @param string $dbname MySQL database name
   533 	 * @param string $dbname MySQL database name
   489 	 * @param string $dbhost MySQL database host
   534 	 * @param string $dbhost MySQL database host
   490 	 */
   535 	 */
   491 	function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
   536 	function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
   492 		register_shutdown_function( array( &$this, '__destruct' ) );
   537 		register_shutdown_function( array( $this, '__destruct' ) );
   493 
   538 
   494 		if ( WP_DEBUG )
   539 		if ( WP_DEBUG )
   495 			$this->show_errors();
   540 			$this->show_errors();
   496 
   541 
   497 		$this->init_charset();
   542 		$this->init_charset();
   511 	 * @since 2.0.8
   556 	 * @since 2.0.8
   512 	 * @return bool true
   557 	 * @return bool true
   513 	 */
   558 	 */
   514 	function __destruct() {
   559 	function __destruct() {
   515 		return true;
   560 		return true;
       
   561 	}
       
   562 
       
   563 	/**
       
   564 	 * PHP5 style magic getter, used to lazy-load expensive data.
       
   565 	 *
       
   566 	 * @since 3.5.0
       
   567 	 *
       
   568 	 * @param string $name The private member to get, and optionally process
       
   569 	 * @return mixed The private member
       
   570 	 */
       
   571 	function __get( $name ) {
       
   572 		if ( 'col_info' == $name )
       
   573 			$this->load_col_info();
       
   574 
       
   575 		return $this->$name;
       
   576 	}
       
   577 
       
   578 	/**
       
   579 	 * Magic function, for backwards compatibility
       
   580 	 *
       
   581 	 * @since 3.5.0
       
   582 	 *
       
   583 	 * @param string $name  The private member to set
       
   584 	 * @param mixed  $value The value to set
       
   585 	 */
       
   586 	function __set( $name, $value ) {
       
   587 		$this->$name = $value;
       
   588 	}
       
   589 
       
   590 	/**
       
   591 	 * Magic function, for backwards compatibility
       
   592 	 *
       
   593 	 * @since 3.5.0
       
   594 	 *
       
   595 	 * @param string $name  The private member to check
       
   596 	 *
       
   597 	 * @return bool If the member is set or not
       
   598 	 */
       
   599 	function __isset( $name ) {
       
   600 		return isset( $this->$name );
       
   601 	}
       
   602 
       
   603 	/**
       
   604 	 * Magic function, for backwards compatibility
       
   605 	 *
       
   606 	 * @since 3.5.0
       
   607 	 *
       
   608 	 * @param string $name  The private member to unset
       
   609 	 */
       
   610 	function __unset( $name ) {
       
   611 		unset( $this->$name );
   516 	}
   612 	}
   517 
   613 
   518 	/**
   614 	/**
   519 	 * Set $this->charset and $this->collate
   615 	 * Set $this->charset and $this->collate
   520 	 *
   616 	 *
   848 	 * @since 2.3.0
   944 	 * @since 2.3.0
   849 	 * @param string $string to escape
   945 	 * @param string $string to escape
   850 	 * @return void
   946 	 * @return void
   851 	 */
   947 	 */
   852 	function escape_by_ref( &$string ) {
   948 	function escape_by_ref( &$string ) {
   853 		$string = $this->_real_escape( $string );
   949 		if ( ! is_float( $string ) )
       
   950 			$string = $this->_real_escape( $string );
   854 	}
   951 	}
   855 
   952 
   856 	/**
   953 	/**
   857 	 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
   954 	 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
   858 	 *
   955 	 *
   888 	 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like
   985 	 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like
   889 	 * 	{@link http://php.net/sprintf sprintf()}.
   986 	 * 	{@link http://php.net/sprintf sprintf()}.
   890 	 * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
   987 	 * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
   891 	 * 	if there was something to prepare
   988 	 * 	if there was something to prepare
   892 	 */
   989 	 */
   893 	function prepare( $query = null ) { // ( $query, *$args )
   990 	function prepare( $query, $args ) {
   894 		if ( is_null( $query ) )
   991 		if ( is_null( $query ) )
   895 			return;
   992 			return;
   896 
   993 
   897 		$args = func_get_args();
   994 		$args = func_get_args();
   898 		array_shift( $args );
   995 		array_shift( $args );
   899 		// If args were passed as an array (as in vsprintf), move them up
   996 		// If args were passed as an array (as in vsprintf), move them up
   900 		if ( isset( $args[0] ) && is_array($args[0]) )
   997 		if ( isset( $args[0] ) && is_array($args[0]) )
   901 			$args = $args[0];
   998 			$args = $args[0];
   902 		$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
   999 		$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
   903 		$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
  1000 		$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
       
  1001 		$query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
   904 		$query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
  1002 		$query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
   905 		array_walk( $args, array( &$this, 'escape_by_ref' ) );
  1003 		array_walk( $args, array( $this, 'escape_by_ref' ) );
   906 		return @vsprintf( $query, $args );
  1004 		return @vsprintf( $query, $args );
   907 	}
  1005 	}
   908 
  1006 
   909 	/**
  1007 	/**
   910 	 * Print SQL/DB error.
  1008 	 * Print SQL/DB error.
   930 		if ( $caller = $this->get_caller() )
  1028 		if ( $caller = $this->get_caller() )
   931 			$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller );
  1029 			$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller );
   932 		else
  1030 		else
   933 			$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query );
  1031 			$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query );
   934 
  1032 
   935 		if ( function_exists( 'error_log' )
  1033 		error_log( $error_str );
   936 			&& ( $log_file = @ini_get( 'error_log' ) )
       
   937 			&& ( 'syslog' == $log_file || @is_writable( $log_file ) )
       
   938 			)
       
   939 			@error_log( $error_str );
       
   940 
  1034 
   941 		// Are we showing errors?
  1035 		// Are we showing errors?
   942 		if ( ! $this->show_errors )
  1036 		if ( ! $this->show_errors )
   943 			return false;
  1037 			return false;
   944 
  1038 
  1021 	 */
  1115 	 */
  1022 	function flush() {
  1116 	function flush() {
  1023 		$this->last_result = array();
  1117 		$this->last_result = array();
  1024 		$this->col_info    = null;
  1118 		$this->col_info    = null;
  1025 		$this->last_query  = null;
  1119 		$this->last_query  = null;
       
  1120 
       
  1121 		if ( is_resource( $this->result ) )
       
  1122 			mysql_free_result( $this->result );
  1026 	}
  1123 	}
  1027 
  1124 
  1028 	/**
  1125 	/**
  1029 	 * Connect to and select database
  1126 	 * Connect to and select database
  1030 	 *
  1127 	 *
  1032 	 */
  1129 	 */
  1033 	function db_connect() {
  1130 	function db_connect() {
  1034 
  1131 
  1035 		$this->is_mysql = true;
  1132 		$this->is_mysql = true;
  1036 
  1133 
       
  1134 		$new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
       
  1135 		$client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
       
  1136 
  1037 		if ( WP_DEBUG ) {
  1137 		if ( WP_DEBUG ) {
  1038 			$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
  1138 			$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
  1039 		} else {
  1139 		} else {
  1040 			$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
  1140 			$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
  1041 		}
  1141 		}
  1042 
  1142 
  1043 		if ( !$this->dbh ) {
  1143 		if ( !$this->dbh ) {
  1044 			wp_load_translations_early();
  1144 			wp_load_translations_early();
  1045 			$this->bail( sprintf( __( "
  1145 			$this->bail( sprintf( __( "
  1102 		if ( $this->last_error = mysql_error( $this->dbh ) ) {
  1202 		if ( $this->last_error = mysql_error( $this->dbh ) ) {
  1103 			$this->print_error();
  1203 			$this->print_error();
  1104 			return false;
  1204 			return false;
  1105 		}
  1205 		}
  1106 
  1206 
  1107 		if ( preg_match( '/^\s*(create|alter|truncate|drop) /i', $query ) ) {
  1207 		if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
  1108 			$return_val = $this->result;
  1208 			$return_val = $this->result;
  1109 		} elseif ( preg_match( '/^\s*(insert|delete|update|replace) /i', $query ) ) {
  1209 		} elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
  1110 			$this->rows_affected = mysql_affected_rows( $this->dbh );
  1210 			$this->rows_affected = mysql_affected_rows( $this->dbh );
  1111 			// Take note of the insert_id
  1211 			// Take note of the insert_id
  1112 			if ( preg_match( '/^\s*(insert|replace) /i', $query ) ) {
  1212 			if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
  1113 				$this->insert_id = mysql_insert_id($this->dbh);
  1213 				$this->insert_id = mysql_insert_id($this->dbh);
  1114 			}
  1214 			}
  1115 			// Return number of rows affected
  1215 			// Return number of rows affected
  1116 			$return_val = $this->rows_affected;
  1216 			$return_val = $this->rows_affected;
  1117 		} else {
  1217 		} else {
  1118 			$i = 0;
       
  1119 			while ( $i < @mysql_num_fields( $this->result ) ) {
       
  1120 				$this->col_info[$i] = @mysql_fetch_field( $this->result );
       
  1121 				$i++;
       
  1122 			}
       
  1123 			$num_rows = 0;
  1218 			$num_rows = 0;
  1124 			while ( $row = @mysql_fetch_object( $this->result ) ) {
  1219 			while ( $row = @mysql_fetch_object( $this->result ) ) {
  1125 				$this->last_result[$num_rows] = $row;
  1220 				$this->last_result[$num_rows] = $row;
  1126 				$num_rows++;
  1221 				$num_rows++;
  1127 			}
  1222 			}
  1128 
       
  1129 			@mysql_free_result( $this->result );
       
  1130 
  1223 
  1131 			// Log number of rows the query returned
  1224 			// Log number of rows the query returned
  1132 			// and return number of rows selected
  1225 			// and return number of rows selected
  1133 			$this->num_rows = $num_rows;
  1226 			$this->num_rows = $num_rows;
  1134 			$return_val     = $num_rows;
  1227 			$return_val     = $num_rows;
  1454 		}
  1547 		}
  1455 		return null;
  1548 		return null;
  1456 	}
  1549 	}
  1457 
  1550 
  1458 	/**
  1551 	/**
       
  1552 	 * Load the column metadata from the last query.
       
  1553 	 *
       
  1554 	 * @since 3.5.0
       
  1555 	 *
       
  1556 	 * @access protected
       
  1557 	 */
       
  1558 	protected function load_col_info() {
       
  1559 		if ( $this->col_info )
       
  1560 			return;
       
  1561 
       
  1562 		for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
       
  1563 			$this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
       
  1564 		}
       
  1565 	}
       
  1566 
       
  1567 	/**
  1459 	 * Retrieve column metadata from the last query.
  1568 	 * Retrieve column metadata from the last query.
  1460 	 *
  1569 	 *
  1461 	 * @since 0.71
  1570 	 * @since 0.71
  1462 	 *
  1571 	 *
  1463 	 * @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
  1572 	 * @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
  1464 	 * @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
  1573 	 * @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
  1465 	 * @return mixed Column Results
  1574 	 * @return mixed Column Results
  1466 	 */
  1575 	 */
  1467 	function get_col_info( $info_type = 'name', $col_offset = -1 ) {
  1576 	function get_col_info( $info_type = 'name', $col_offset = -1 ) {
       
  1577 		$this->load_col_info();
       
  1578 
  1468 		if ( $this->col_info ) {
  1579 		if ( $this->col_info ) {
  1469 			if ( $col_offset == -1 ) {
  1580 			if ( $col_offset == -1 ) {
  1470 				$i = 0;
  1581 				$i = 0;
  1471 				$new_array = array();
  1582 				$new_array = array();
  1472 				foreach( (array) $this->col_info as $col ) {
  1583 				foreach( (array) $this->col_info as $col ) {
  1504 	}
  1615 	}
  1505 
  1616 
  1506 	/**
  1617 	/**
  1507 	 * Wraps errors in a nice header and footer and dies.
  1618 	 * Wraps errors in a nice header and footer and dies.
  1508 	 *
  1619 	 *
  1509 	 * Will not die if wpdb::$show_errors is true
  1620 	 * Will not die if wpdb::$show_errors is false.
  1510 	 *
  1621 	 *
  1511 	 * @since 1.5.0
  1622 	 * @since 1.5.0
  1512 	 *
  1623 	 *
  1513 	 * @param string $message The Error message
  1624 	 * @param string $message The Error message
  1514 	 * @param string $error_code Optional. A Computer readable string to identify the error.
  1625 	 * @param string $error_code Optional. A Computer readable string to identify the error.
  1545 	 * Whether the database supports collation.
  1656 	 * Whether the database supports collation.
  1546 	 *
  1657 	 *
  1547 	 * Called when WordPress is generating the table scheme.
  1658 	 * Called when WordPress is generating the table scheme.
  1548 	 *
  1659 	 *
  1549 	 * @since 2.5.0
  1660 	 * @since 2.5.0
       
  1661 	 * @deprecated 3.5.0
       
  1662 	 * @deprecated Use wpdb::has_cap( 'collation' )
  1550 	 *
  1663 	 *
  1551 	 * @return bool True if collation is supported, false if version does not
  1664 	 * @return bool True if collation is supported, false if version does not
  1552 	 */
  1665 	 */
  1553 	function supports_collation() {
  1666 	function supports_collation() {
       
  1667 		_deprecated_function( __FUNCTION__, '3.5', 'wpdb::has_cap( \'collation\' )' );
  1554 		return $this->has_cap( 'collation' );
  1668 		return $this->has_cap( 'collation' );
       
  1669 	}
       
  1670 
       
  1671 	/**
       
  1672 	 * The database character collate.
       
  1673 	 *
       
  1674 	 * @since 3.5.0
       
  1675 	 *
       
  1676 	 * @return string The database character collate.
       
  1677 	 */
       
  1678 	public function get_charset_collate() {
       
  1679 		$charset_collate = '';
       
  1680 
       
  1681 		if ( ! empty( $this->charset ) )
       
  1682 			$charset_collate = "DEFAULT CHARACTER SET $this->charset";
       
  1683 		if ( ! empty( $this->collate ) )
       
  1684 			$charset_collate .= " COLLATE $this->collate";
       
  1685 
       
  1686 		return $charset_collate;
  1555 	}
  1687 	}
  1556 
  1688 
  1557 	/**
  1689 	/**
  1558 	 * Determine if a database supports a particular feature
  1690 	 * Determine if a database supports a particular feature
  1559 	 *
  1691 	 *