wpdb::query() WordPress Method
The wpdb::query() method is used to execute a SQL query on the WordPress database. This method should be used when you need to execute a SQL query that is not a SELECT, UPDATE, DELETE, or INSERT query. For example, if you need to create or drop a database table, you would use the wpdb::query() method.
wpdb::query( string $query ) #
Performs a database query, using current database connection.
Description
More information can be found on the documentation page.
Parameters
- $query
(string)(Required)Database query.
Return
(int|bool) Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows affected/selected for all other queries. Boolean false on error.
Source
File: wp-includes/wp-db.php
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 | public function query( $query ) { if ( ! $this ->ready ) { $this ->check_current_query = true; return false; } /** * 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. */ $query = apply_filters( 'query' , $query ); if ( ! $query ) { $this ->insert_id = 0; return false; } $this -> flush (); // Log how the function was called. $this ->func_call = "\$db->query(\"$query\")" ; // If we're writing to the database, make sure the query will write safely. if ( $this ->check_current_query && ! $this ->check_ascii( $query ) ) { $stripped_query = $this ->strip_invalid_text_from_query( $query ); // strip_invalid_text_from_query() can perform queries, so we need // to flush again, just to make sure everything is clear. $this -> flush (); if ( $stripped_query !== $query ) { $this ->insert_id = 0; $this ->last_query = $query ; wp_load_translations_early(); $this ->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' ); return false; } } $this ->check_current_query = true; // Keep track of the last query for debug. $this ->last_query = $query ; $this ->_do_query( $query ); // Database server has gone away, try to reconnect. $mysql_errno = 0; if ( ! empty ( $this ->dbh ) ) { if ( $this ->use_mysqli ) { if ( $this ->dbh instanceof mysqli ) { $mysql_errno = mysqli_errno( $this ->dbh ); } else { // $dbh is defined, but isn't a real connection. // Something has gone horribly wrong, let's try a reconnect. $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 there is an error then take note of it. 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 ) { // Clear insert_id on a subsequent failed insert. 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 ); } // Take note of the insert_id. 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 number of rows affected. $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 ++; } } // Log and return the number of rows selected. $this ->num_rows = $num_rows ; $return_val = $num_rows ; } return $return_val ; } |
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
0.71 | Introduced. |