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