wp-includes/wp-db.php
/**
	 * Perform a MySQL database query, using current database connection.
	 *
	 * More information can be found on the codex page.
	 *
	 * @since 0.71
	 *
	 * @param string $query Database query
	 * @return int|false Number of rows affected/selected or false on error
	 */
Perform a MySQL database query, using current database connection.
More information can be found on the codex page.



@param string $query Database query
@return int|false Number of rows affected/selected or false on error
Perform a MySQL database query using current database connection . More information can be found on the codex page . @param string $query Database query @return int|false Number of rows affected / selected or false on error
public function query( $query ) {
		if ( ! $this->ready ) {
			$this->check_current_query = true;
			return false;
		}
		$query = apply_filters( 'query', $query );
		$this->flush();
		$this->func_call = "\$db->query(\"$query\")";
		if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
			$stripped_query = $this->strip_invalid_text_from_query( $query );
			$this->flush();
			if ( $stripped_query !== $query ) {
				$this->insert_id = 0;
				return false;
			}
		}
		$this->check_current_query = true;
		$this->last_query = $query;
		$this->_do_query( $query );
		$mysql_errno = 0;
		if ( ! empty( $this->dbh ) ) {
			if ( $this->use_mysqli ) {
				if ( $this->dbh instanceof mysqli ) {
					$mysql_errno = mysqli_errno( $this->dbh );
				} else {
					$mysql_errno = 2006;
				}
			} else {
				if ( is_resource( $this->dbh ) ) {
					$mysql_errno = mysql_errno( $this->dbh );
				} else {
					$mysql_errno = 2006;
				}
			}
		}
		if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
			if ( $this->check_connection() ) {
				$this->_do_query( $query );
			} else {
				$this->insert_id = 0;
				return false;
			}
		}
		if ( $this->use_mysqli ) {
			if ( $this->dbh instanceof mysqli ) {
				$this->last_error = mysqli_error( $this->dbh );
			} else {
				$this->last_error = __( 'Unable to retrieve the error message from MySQL' );
			}
		} else {
			if ( is_resource( $this->dbh ) ) {
				$this->last_error = mysql_error( $this->dbh );
			} else {
				$this->last_error = __( 'Unable to retrieve the error message from MySQL' );
			}
		}
		if ( $this->last_error ) {
			if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
				$this->insert_id = 0;
			$this->print_error();
			return false;
		}
		if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
			$return_val = $this->result;
		} elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
			if ( $this->use_mysqli ) {
				$this->rows_affected = mysqli_affected_rows( $this->dbh );
			} else {
				$this->rows_affected = mysql_affected_rows( $this->dbh );
			}
			if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
				if ( $this->use_mysqli ) {
					$this->insert_id = mysqli_insert_id( $this->dbh );
				} else {
					$this->insert_id = mysql_insert_id( $this->dbh );
				}
			}
			$return_val = $this->rows_affected;
		} else {
			$num_rows = 0;
			if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
				while ( $row = mysqli_fetch_object( $this->result ) ) {
					$this->last_result[$num_rows] = $row;
					$num_rows++;
				}
			} elseif ( is_resource( $this->result ) ) {
				while ( $row = mysql_fetch_object( $this->result ) ) {
					$this->last_result[$num_rows] = $row;
					$num_rows++;
				}
			}
			$this->num_rows = $num_rows;
			$return_val     = $num_rows;
		}
		return $return_val;
	}
public function query ( $ query ) { if ( ! $ this -> ready ) { $ this -> check_current_query = true ; return false ; } $ query = apply_filters ( 'query' , $ query ) ; $ this -> flush ( ) ; $ this -> func_call = " \$ db->query( \" $ query \" ) " ; if ( $ this -> check_current_query && ! $ this -> check_ascii ( $ query ) ) { $ stripped_query = $ this -> strip_invalid_text_from_query ( $ query ) ; $ this -> flush ( ) ; if ( $ stripped_query !== $ query ) { $ this -> insert_id = 0 ; return false ; } } $ this -> check_current_query = true ; $ this -> last_query = $ query ; $ this -> _do_query ( $ query ) ; $ mysql_errno = 0 ; if ( ! empty ( $ this -> dbh ) ) { if ( $ this -> use_mysqli ) { if ( $ this -> dbh instanceof mysqli ) { $ mysql_errno = mysqli_errno ( $ this -> dbh ) ; } else { $ mysql_errno = 2006 ; } } else { if ( is_resource ( $ this -> dbh ) ) { $ mysql_errno = mysql_errno ( $ this -> dbh ) ; } else { $ mysql_errno = 2006 ; } } } if ( empty ( $ this -> dbh ) || 2006 == $ mysql_errno ) { if ( $ this -> check_connection ( ) ) { $ this -> _do_query ( $ query ) ; } else { $ this -> insert_id = 0 ; return false ; } } if ( $ this -> use_mysqli ) { if ( $ this -> dbh instanceof mysqli ) { $ this -> last_error = mysqli_error ( $ this -> dbh ) ; } else { $ this -> last_error = __ ( 'Unable to retrieve the error message from MySQL' ) ; } } else { if ( is_resource ( $ this -> dbh ) ) { $ this -> last_error = mysql_error ( $ this -> dbh ) ; } else { $ this -> last_error = __ ( 'Unable to retrieve the error message from MySQL' ) ; } } if ( $ this -> last_error ) { if ( $ this -> insert_id && preg_match ( '/^\s*(insert|replace)\s/i' , $ query ) ) $ this -> insert_id = 0 ; $ this -> print_error ( ) ; return false ; } if ( preg_match ( '/^\s*(create|alter|truncate|drop)\s/i' , $ query ) ) { $ return_val = $ this -> result ; } elseif ( preg_match ( '/^\s*(insert|delete|update|replace)\s/i' , $ query ) ) { if ( $ this -> use_mysqli ) { $ this -> rows_affected = mysqli_affected_rows ( $ this -> dbh ) ; } else { $ this -> rows_affected = mysql_affected_rows ( $ this -> dbh ) ; } if ( preg_match ( '/^\s*(insert|replace)\s/i' , $ query ) ) { if ( $ this -> use_mysqli ) { $ this -> insert_id = mysqli_insert_id ( $ this -> dbh ) ; } else { $ this -> insert_id = mysql_insert_id ( $ this -> dbh ) ; } } $ return_val = $ this -> rows_affected ; } else { $ num_rows = 0 ; if ( $ this -> use_mysqli && $ this -> result instanceof mysqli_result ) { while ( $ row = mysqli_fetch_object ( $ this -> result ) ) { $ this -> last_result [ $ num_rows ] = $ row ; $ num_rows ++ ; } } elseif ( is_resource ( $ this -> result ) ) { while ( $ row = mysql_fetch_object ( $ this -> result ) ) { $ this -> last_result [ $ num_rows ] = $ row ; $ num_rows ++ ; } } $ this -> num_rows = $ num_rows ; $ return_val = $ num_rows ; } return $ return_val ; }
Perform a MySQL database query, using current database connection.
Perform a MySQL database query using current database connection .
/**
		 * Filters the database query.
		 *
		 * Some queries are made before the plugins have been loaded,
		 * and thus cannot be filtered with this method.
		 *
		 * @since 2.1.0
		 *
		 * @param string $query Database query.
		 */ // Log how the function was called // If we're writing to the database, make sure the query will write safely. // strip_invalid_text_from_query() can perform queries, so we need // to flush again, just to make sure everything is clear. // Keep track of the last query for debug. // MySQL server has gone away, try to reconnect. // $dbh is defined, but isn't a real connection. // Something has gone horribly wrong, let's try a reconnect. // If there is an error then take note of it. // Clear insert_id on a subsequent failed insert. // Take note of the insert_id // Return number of rows affected // Log number of rows the query returned // and return number of rows selected