Ticket #41956: 41956.4.diff
File 41956.4.diff, 4.7 KB (added by , 7 years ago) |
---|
-
src/wp-includes/wp-db.php
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 … … 1311 1326 public function print_error( $str = '' ) { 1312 1327 global $EZSQL_ERROR; 1313 1328 1314 if ( !$str ) { 1315 if ( $this->use_mysqli ) { 1316 $str = mysqli_error( $this->dbh ); 1317 } else { 1318 $str = mysql_error( $this->dbh ); 1319 } 1329 if ( '' === $str ) { 1330 $str = $this->last_err_msg; 1320 1331 } 1321 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );1332 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str, 'error_no' => $this->last_err_no ); 1322 1333 1323 1334 if ( $this->suppress_errors ) 1324 1335 return false; … … 1368 1379 } 1369 1380 1370 1381 /** 1382 * Retrieve error code from the last database error. 1383 * 1384 * @since 1385 * @return int Error code, Zero on no error has occurred. 1386 */ 1387 public function get_err_no(){ 1388 return $this->last_err_no; 1389 } 1390 1391 /** 1392 * Retrieve error message from the last database error. 1393 * 1394 * @since 1395 * @return string Error message, empty string on no error has occurred. 1396 */ 1397 public function get_err_msg(){ 1398 return $this->last_err_msg; 1399 } 1400 1401 /** 1402 * Retrieve error info from the last database error. 1403 * 1404 * @since 1405 * @return array|bool Array containing error no and message, false on no error has occurred. 1406 */ 1407 public function get_last_error(){ 1408 if ( $this->last_err_no > 0 ){ 1409 return array( 1410 'error_no' => $this->last_err_no, 1411 'error_message' => $this->last_err_msg, 1412 ); 1413 } else { 1414 return false; 1415 } 1416 } 1417 1418 /** 1371 1419 * Enables showing of database errors. 1372 1420 * 1373 1421 * This function should be used only to enable showing of errors. … … 1430 1478 $this->col_info = null; 1431 1479 $this->last_query = null; 1432 1480 $this->rows_affected = $this->num_rows = 0; 1481 $this->last_err_no = 0; 1482 $this->last_err_msg = ''; 1433 1483 $this->last_error = ''; 1434 1484 1435 1485 if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { … … 1772 1822 $this->_do_query( $query ); 1773 1823 1774 1824 // MySQL server has gone away, try to reconnect. 1775 $ mysql_errno = 0;1825 $this->last_err_no = 0; 1776 1826 if ( ! empty( $this->dbh ) ) { 1777 1827 if ( $this->use_mysqli ) { 1778 1828 if ( $this->dbh instanceof mysqli ) { 1779 $ mysql_errno = mysqli_errno( $this->dbh );1829 $this->last_err_no = mysqli_errno( $this->dbh ); 1780 1830 } else { 1781 1831 // $dbh is defined, but isn't a real connection. 1782 1832 // Something has gone horribly wrong, let's try a reconnect. 1783 $ mysql_errno = 2006;1833 $this->last_err_no = 2006; 1784 1834 } 1785 1835 } else { 1786 1836 if ( is_resource( $this->dbh ) ) { 1787 $ mysql_errno = mysql_errno( $this->dbh );1837 $this->last_err_no = mysql_errno( $this->dbh ); 1788 1838 } else { 1789 $ mysql_errno = 2006;1839 $this->last_err_no = 2006; 1790 1840 } 1791 1841 } 1792 1842 } 1793 1843 1794 if ( empty( $this->dbh ) || 2006 == $ mysql_errno ) {1844 if ( empty( $this->dbh ) || 2006 == $this->last_err_no ) { 1795 1845 if ( $this->check_connection() ) { 1796 1846 $this->_do_query( $query ); 1797 1847 } else { … … 1798 1848 $this->insert_id = 0; 1799 1849 return false; 1800 1850 } 1801 } 1802 1803 // If there is an error then take note of it. 1804 if ( $this->use_mysqli ) { 1805 if ( $this->dbh instanceof mysqli ) { 1806 $this->last_error = mysqli_error( $this->dbh ); 1851 } elseif ( 0 !== $this->last_err_no ) { 1852 // If there is an error then take note of it. 1853 if ( $this->use_mysqli ) { 1854 if ( $this->dbh instanceof mysqli ) { 1855 $this->last_error = mysqli_error( $this->dbh ); 1856 $this->last_err_msg = mysqli_error( $this->dbh ); 1857 } else { 1858 $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); 1859 } 1807 1860 } else { 1808 $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); 1861 if ( is_resource( $this->dbh ) ) { 1862 $this->last_error = mysql_error( $this->dbh ); 1863 $this->last_err_msg = mysql_error( $this->dbh ); 1864 } else { 1865 $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); 1866 } 1809 1867 } 1810 } else {1811 if ( is_resource( $this->dbh ) ) {1812 $this->last_error = mysql_error( $this->dbh );1813 } else {1814 $this->last_error = __( 'Unable to retrieve the error message from MySQL' );1815 }1816 }1817 1868 1818 if ( $this->last_error ) {1819 1869 // Clear insert_id on a subsequent failed insert. 1820 1870 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) 1821 1871 $this->insert_id = 0;