wpdb::query( string $query )
Perform a MySQL database query, using current database connection.
Description Description
More information can be found on the codex page.
Parameters Parameters
- $query
-
(string) (Required) Database query
Return 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 Source
File: wp-includes/wp-db.php
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 | 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 ); $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; return false; } } $this ->check_current_query = true; // Keep track of the last query for debug. $this ->last_query = $query ; $this ->_do_query( $query ); // MySQL 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 number of rows the query returned // and return number of rows selected $this ->num_rows = $num_rows ; $return_val = $num_rows ; } return $return_val ; } |
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
0.71 | Introduced. |