* the actual setting up of the class properties and connection
* to the database.
*
- * @link http://core.trac.wordpress.org/ticket/3354
+ * @link https://core.trac.wordpress.org/ticket/3354
* @since 2.0.8
*
* @param string $dbuser MySQL database user
* @return mixed The private member
*/
public function __get( $name ) {
- if ( 'col_info' == $name )
+ if ( 'col_info' === $name )
$this->load_col_info();
return $this->$name;
* @since 3.1.0
*
* @param resource $dbh The resource given by mysql_connect
- * @param string $charset The character set (optional)
- * @param string $collate The collation (optional)
+ * @param string $charset Optional. The character set. Default null.
+ * @param string $collate Optional. The collation. Default null.
*/
public function set_charset( $dbh, $charset = null, $collate = null ) {
if ( ! isset( $charset ) )
*
* @since 3.9.0
*
- * @see wpdb::$incompatible_modes
- *
* @param array $incompatible_modes An array of incompatible modes.
*/
$incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
* @access public
* @param int $blog_id
* @param int $site_id Optional.
- * @return string previous blog id
+ * @return int previous blog id
*/
public function set_blog_id( $blog_id, $site_id = 0 ) {
if ( ! empty( $site_id ) )
/**
* Gets blog prefix.
*
- * @uses is_multisite()
* @since 3.0.0
* @param int $blog_id Optional.
* @return string Blog prefix.
* @uses wpdb::$old_tables
* @uses wpdb::$global_tables
* @uses wpdb::$ms_global_tables
- * @uses is_multisite()
*
* @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
* @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
}
$class = get_class( $this );
- _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE );
+ if ( function_exists( '__' ) ) {
+ _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), E_USER_NOTICE );
+ } else {
+ _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), E_USER_NOTICE );
+ }
return addslashes( $string );
}
*
* Both %d and %s should be left unquoted in the query string.
*
- * <code>
- * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
- * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
- * </code>
+ * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
+ * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
*
* @link http://php.net/sprintf Description of syntax.
* @since 2.3.0
* @global array $EZSQL_ERROR Stores error information of query and error string
*
* @param string $str The error to display
- * @return bool False if the showing of errors is disabled.
+ * @return false|null False if the showing of errors is disabled.
*/
public function print_error( $str = '' ) {
global $EZSQL_ERROR;
$this->rows_affected = $this->num_rows = 0;
$this->last_error = '';
- if ( is_resource( $this->result ) ) {
- if ( $this->use_mysqli ) {
- mysqli_free_result( $this->result );
- } else {
- mysql_free_result( $this->result );
+ if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
+ mysqli_free_result( $this->result );
+ $this->result = null;
+
+ // Sanity check before using the handle
+ if ( empty( $this->dbh ) || !( $this->dbh instanceof mysqli ) ) {
+ return;
}
+
+ // Clear out any results from a multi-query
+ while ( mysqli_more_results( $this->dbh ) ) {
+ mysqli_next_result( $this->dbh );
+ }
+ } else if ( is_resource( $this->result ) ) {
+ mysql_free_result( $this->result );
}
}
* @since 3.9.0 $allow_bail parameter added.
*
* @param bool $allow_bail Optional. Allows the function to bail. Default true.
- * @return bool True with a successful connection, false on failure.
+ * @return null|bool True with a successful connection, false on failure.
*/
public function db_connect( $allow_bail = true ) {
* @since 3.9.0
*
* @param bool $allow_bail Optional. Allows the function to bail. Default true.
- * @return bool True if the connection is up.
+ * @return bool|null True if the connection is up.
*/
public function check_connection( $allow_bail = true ) {
if ( $this->use_mysqli ) {
* @return int|false Number of rows affected/selected or false on error
*/
public function query( $query ) {
- if ( ! $this->ready )
+ if ( ! $this->ready ) {
return false;
+ }
/**
* Filter the database query.
$return_val = $this->rows_affected;
} else {
$num_rows = 0;
- if ( $this->use_mysqli ) {
+ if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
while ( $row = @mysqli_fetch_object( $this->result ) ) {
$this->last_result[$num_rows] = $row;
$num_rows++;
}
- } else {
+ } else if ( is_resource( $this->result ) ) {
while ( $row = @mysql_fetch_object( $this->result ) ) {
$this->last_result[$num_rows] = $row;
$num_rows++;
/**
* Insert a row into a table.
*
- * <code>
- * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
- * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
- * </code>
+ * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
+ * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
*
* @since 2.5.0
* @see wpdb::prepare()
/**
* Replace a row into a table.
*
- * <code>
- * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
- * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
- * </code>
+ * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
+ * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
*
* @since 3.0.0
* @see wpdb::prepare()
/**
* Update a row in the table
*
- * <code>
- * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
- * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
- * </code>
+ * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
+ * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
*
* @since 2.5.0
* @see wpdb::prepare()
/**
* Delete a row in the table
*
- * <code>
- * wpdb::delete( 'table', array( 'ID' => 1 ) )
- * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
- * </code>
+ * wpdb::delete( 'table', array( 'ID' => 1 ) )
+ * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
*
* @since 3.4.0
* @see wpdb::prepare()
return $this->query( $this->prepare( $sql, $where ) );
}
-
/**
* Retrieve one variable from the database.
*
*/
public function get_var( $query = null, $x = 0, $y = 0 ) {
$this->func_call = "\$db->get_var(\"$query\", $x, $y)";
- if ( $query )
+
+ if ( $query ) {
$this->query( $query );
+ }
// Extract var out of cached results based x,y vals
if ( !empty( $this->last_result[$y] ) ) {
*/
public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
- if ( $query )
+ if ( $query ) {
$this->query( $query );
- else
+ } else {
return null;
+ }
if ( !isset( $this->last_result[$y] ) )
return null;
* @return array Database query result. Array indexed from 0 by SQL result row number.
*/
public function get_col( $query = null , $x = 0 ) {
- if ( $query )
+ if ( $query ) {
$this->query( $query );
+ }
$new_array = array();
// Extract the column values
public function get_results( $query = null, $output = OBJECT ) {
$this->func_call = "\$db->get_results(\"$query\", $output)";
- if ( $query )
+ if ( $query ) {
$this->query( $query );
- else
+ } else {
return null;
+ }
$new_array = array();
if ( $output == OBJECT ) {
*
* @since 1.5.0
*
- * @return true
+ * @return bool
*/
public function timer_start() {
$this->time_start = microtime( true );
* Determine if a database supports a particular feature.
*
* @since 2.7.0
+ * @since 4.1.0 Support was added for the 'utf8mb4' feature.
+ *
* @see wpdb::db_version()
*
- * @param string $db_cap The feature to check for.
- * @return bool
+ * @param string $db_cap The feature to check for. Accepts 'collation',
+ * 'group_concat', 'subqueries', 'set_charset',
+ * or 'utf8mb4'.
+ * @return bool Whether the database feature is supported, false otherwise.
*/
public function has_cap( $db_cap ) {
$version = $this->db_version();
return version_compare( $version, '4.1', '>=' );
case 'set_charset' :
return version_compare( $version, '5.0.7', '>=' );
- };
+ case 'utf8mb4' : // @since 4.1.0
+ return version_compare( $version, '5.5.3', '>=' );
+ }
return false;
}
*
* @since 2.7.0
*
- * @return false|string false on failure, version number on success
+ * @return null|string Null on failure, version number on success.
*/
public function db_version() {
if ( $this->use_mysqli ) {