Ticket #41956: 41956.5.diff
File 41956.5.diff, 8.3 KB (added by , 7 years ago) |
---|
-
src/wp-includes/wp-db.php
diff --git src/wp-includes/wp-db.php src/wp-includes/wp-db.php index 77b64da..6601a72 100644
class wpdb { 77 77 public $last_error = ''; 78 78 79 79 /** 80 * The last error code during query. 81 * 82 * @since ?.?.? 83 * @var int 84 */ 85 protected $last_err_no = 0; 86 87 /** 88 * The last error message during query. 89 * 90 * @var string 91 */ 92 protected $last_err_msg = ''; 93 94 /** 80 95 * Amount of queries made 81 96 * 82 97 * @since 1.2.0 … … class wpdb { 1419 1434 public function print_error( $str = '' ) { 1420 1435 global $EZSQL_ERROR; 1421 1436 1422 if ( ! $str ) { 1423 if ( $this->use_mysqli ) { 1424 $str = mysqli_error( $this->dbh ); 1425 } else { 1426 $str = mysql_error( $this->dbh ); 1427 } 1437 if ( '' === $str ) { 1438 $str = $this->last_err_msg; 1428 1439 } 1429 1440 $EZSQL_ERROR[] = array( 1430 1441 'query' => $this->last_query, 1431 1442 'error_str' => $str, 1443 'error_no' => $this->last_err_no, 1432 1444 ); 1433 1445 1434 1446 if ( $this->suppress_errors ) { … … class wpdb { 1481 1493 } 1482 1494 1483 1495 /** 1496 * Retrieve error code from the last database error. 1497 * 1498 * @since ?.?.? 1499 * 1500 * @return int Error code, Zero on no error has occurred. 1501 */ 1502 public function get_err_no() { 1503 return $this->last_err_no; 1504 } 1505 1506 /** 1507 * Retrieve error message from the last database error. 1508 * 1509 * @since ?.?.? 1510 * 1511 * @return string Error message, empty string on no error has occurred. 1512 */ 1513 public function get_err_msg() { 1514 return $this->last_err_msg; 1515 } 1516 1517 /** 1518 * Retrieve error info from the last database error. 1519 * 1520 * @since ?.?.? 1521 * 1522 * @return array|bool Array containing error no and message, false on no error has occurred. 1523 */ 1524 public function get_last_error() { 1525 if ( $this->last_err_no > 0 ) { 1526 return array( 1527 'error_no' => $this->last_err_no, 1528 'error_message' => $this->last_err_msg, 1529 ); 1530 } else { 1531 return false; 1532 } 1533 } 1534 1535 /** 1484 1536 * Enables showing of database errors. 1485 1537 * 1486 1538 * This function should be used only to enable showing of errors. … … class wpdb { 1544 1596 $this->last_query = null; 1545 1597 $this->rows_affected = $this->num_rows = 0; 1546 1598 $this->last_error = ''; 1599 $this->last_err_no = 0; 1600 $this->last_err_msg = ''; 1547 1601 1548 1602 if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { 1549 1603 mysqli_free_result( $this->result ); … … class wpdb { 1893 1947 $this->_do_query( $query ); 1894 1948 1895 1949 // MySQL server has gone away, try to reconnect. 1896 $ mysql_errno = 0;1950 $this->last_err_no = 0; 1897 1951 if ( ! empty( $this->dbh ) ) { 1898 1952 if ( $this->use_mysqli ) { 1899 1953 if ( $this->dbh instanceof mysqli ) { 1900 $ mysql_errno = mysqli_errno( $this->dbh );1954 $this->last_err_no = mysqli_errno( $this->dbh ); 1901 1955 } else { 1902 1956 // $dbh is defined, but isn't a real connection. 1903 1957 // Something has gone horribly wrong, let's try a reconnect. 1904 $ mysql_errno = 2006;1958 $this->last_err_no = 2006; 1905 1959 } 1906 1960 } else { 1907 1961 if ( is_resource( $this->dbh ) ) { 1908 $ mysql_errno = mysql_errno( $this->dbh );1962 $this->last_err_no = mysql_errno( $this->dbh ); 1909 1963 } else { 1910 $ mysql_errno = 2006;1964 $this->last_err_no = 2006; 1911 1965 } 1912 1966 } 1913 1967 } 1914 1968 1915 if ( empty( $this->dbh ) || 2006 == $ mysql_errno ) {1969 if ( empty( $this->dbh ) || 2006 == $this->last_err_no ) { 1916 1970 if ( $this->check_connection() ) { 1917 1971 $this->_do_query( $query ); 1918 1972 } else { 1919 1973 $this->insert_id = 0; 1920 1974 return false; 1921 1975 } 1922 } 1923 1924 // If there is an error then take note of it. 1925 if ( $this->use_mysqli ) { 1926 if ( $this->dbh instanceof mysqli ) { 1927 $this->last_error = mysqli_error( $this->dbh ); 1928 } else { 1929 $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); 1930 } 1931 } else { 1932 if ( is_resource( $this->dbh ) ) { 1933 $this->last_error = mysql_error( $this->dbh ); 1976 } elseif ( 0 !== $this->last_err_no ) { 1977 // If there is an error then take note of it. 1978 if ( $this->use_mysqli ) { 1979 if ( $this->dbh instanceof mysqli ) { 1980 $this->last_error = mysqli_error( $this->dbh ); 1981 $this->last_err_msg = mysqli_error( $this->dbh ); 1982 } else { 1983 $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); 1984 } 1934 1985 } else { 1935 $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); 1986 if ( is_resource( $this->dbh ) ) { 1987 $this->last_error = mysql_error( $this->dbh ); 1988 $this->last_err_msg = mysql_error( $this->dbh ); 1989 } else { 1990 $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); 1991 } 1936 1992 } 1937 }1938 1993 1939 if ( $this->last_error ) {1940 1994 // Clear insert_id on a subsequent failed insert. 1941 1995 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1942 1996 $this->insert_id = 0; -
tests/phpunit/tests/db.php
diff --git tests/phpunit/tests/db.php tests/phpunit/tests/db.php index 5cfd3d4..2978de0 100644
class Tests_DB extends WP_UnitTestCase { 1771 1771 ), 1772 1772 ); 1773 1773 } 1774 1775 /** 1776 * The get_err_no() method should return zero when no errors. 1777 * 1778 * @ticket 41956 1779 */ 1780 public function test_get_err_no_should_return_zero_when_no_errors() { 1781 global $wpdb; 1782 $this->assertSame( 0, $wpdb->get_err_no() ); 1783 } 1784 1785 /** 1786 * The get_err_no() method should return the error code from the last error. 1787 * 1788 * @ticket 41956 1789 */ 1790 public function test_get_err_no_should_return_error_code_from_last_error() { 1791 global $wpdb; 1792 1793 // Generate error code 1054 (ER_BAD_FIELD_ERROR). 1794 $invalid_sql_1054 = "SELECT unknown_column FROM {$wpdb->posts}"; 1795 $wpdb->suppress_errors( true ); 1796 $wpdb->query( $invalid_sql_1054 ); 1797 $wpdb->suppress_errors( false ); 1798 1799 // Generate error code 1064 (ER_PARSE_ERROR). 1800 $invalid_sql_1064 = 'SELECT 2 FROM'; 1801 $wpdb->suppress_errors( true ); 1802 $wpdb->query( $invalid_sql_1064 ); 1803 $wpdb->suppress_errors( false ); 1804 1805 $this->assertSame( 1064, $wpdb->get_err_no() ); 1806 } 1807 1808 /** 1809 * The get_err_msg() method should return an empty string when no errors. 1810 * 1811 * @ticket 41956 1812 */ 1813 public function test_get_err_msg_should_return_empty_string_when_no_errors() { 1814 global $wpdb; 1815 $this->assertSame( '', $wpdb->get_err_msg() ); 1816 } 1817 1818 /** 1819 * The get_err_msg() method should return the error message from the last error. 1820 * 1821 * @ticket 41956 1822 */ 1823 public function test_get_err_msg_should_return_message_from_last_error() { 1824 global $wpdb; 1825 1826 // Generate error code 1064 (ER_PARSE_ERROR) containing the message: %s near '%s' at line %d 1827 $invalid_sql_1064 = 'SELECT 2 FROM'; 1828 $wpdb->suppress_errors( true ); 1829 $wpdb->query( $invalid_sql_1064 ); 1830 $wpdb->suppress_errors( false ); 1831 1832 // Generate error code 1054 (ER_BAD_FIELD_ERROR) containing the message: Unknown column '%s' in '%s'. 1833 $invalid_sql_1054 = "SELECT unknown_column FROM {$wpdb->posts}"; 1834 $wpdb->suppress_errors( true ); 1835 $wpdb->query( $invalid_sql_1054 ); 1836 $wpdb->suppress_errors( false ); 1837 1838 $this->assertContains( 'Unknown column', $wpdb->get_err_msg() ); 1839 } 1840 1841 /** 1842 * The get_last_error() method should return false when no errors. 1843 * 1844 * @ticket 41956 1845 */ 1846 public function test_get_last_error_should_return_false_without_errors() { 1847 global $wpdb; 1848 $this->assertFalse( $wpdb->get_last_error() ); 1849 } 1850 1851 /** 1852 * The get_last_error() method should return an array of error code and message from the last error. 1853 * 1854 * @ticket 41956 1855 */ 1856 public function test_get_last_error_should_return_error_code_and_msg_from_last_error() { 1857 global $wpdb; 1858 1859 // Generate error code 1064 (ER_PARSE_ERROR) containing the message: %s near '%s' at line %d 1860 $invalid_sql_1064 = 'SELECT 2 FROM'; 1861 $wpdb->suppress_errors( true ); 1862 $wpdb->query( $invalid_sql_1064 ); 1863 $wpdb->suppress_errors( false ); 1864 1865 // Generate error code 1054 (ER_BAD_FIELD_ERROR) containing the message: Unknown column '%s' in '%s'. 1866 $invalid_sql_1054 = "SELECT unknown_column FROM {$wpdb->posts}"; 1867 $wpdb->suppress_errors( true ); 1868 $wpdb->query( $invalid_sql_1054 ); 1869 $wpdb->suppress_errors( false ); 1870 1871 $actual = $wpdb->get_last_error(); 1872 1873 $this->assertEquals( $invalid_sql_1054, $wpdb->last_query ); 1874 $this->assertCount( 2, $actual ); 1875 1876 $this->assertArrayHasKey( 'error_no', $actual ); 1877 $this->assertArrayHasKey( 'error_message', $actual ); 1878 1879 $this->assertSame( 1054, $actual['error_no'] ); 1880 $this->assertContains( 'Unknown column', $actual['error_message'] ); 1881 } 1774 1882 }