Make WordPress Core

Ticket #41956: 41956.3.diff

File 41956.3.diff, 4.9 KB (added by munyagu, 7 years ago)
  • src/wp-includes/wp-db.php

     
    7777        public $last_error = '';
    7878
    7979        /**
     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        /**
    8094         * Amount of queries made
    8195         *
    8296         * @since 1.2.0
     
    13111325        public function print_error( $str = '' ) {
    13121326                global $EZSQL_ERROR;
    13131327
    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;
    13201330                }
    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 );
    13221332
    13231333                if ( $this->suppress_errors )
    13241334                        return false;
     
    13681378        }
    13691379
    13701380        /**
     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        /**
    13711418         * Enables showing of database errors.
    13721419         *
    13731420         * This function should be used only to enable showing of errors.
     
    14261473         * @since 0.71
    14271474         */
    14281475        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;
    14321479                $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    = '';
    14341483
    14351484                if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
    14361485                        mysqli_free_result( $this->result );
     
    17721821                $this->_do_query( $query );
    17731822
    17741823                // MySQL server has gone away, try to reconnect.
    1775                 $mysql_errno = 0;
     1824                $this->last_err_no = 0;
    17761825                if ( ! empty( $this->dbh ) ) {
    17771826                        if ( $this->use_mysqli ) {
    17781827                                if ( $this->dbh instanceof mysqli ) {
    1779                                         $mysql_errno = mysqli_errno( $this->dbh );
     1828                                        $this->last_err_no = mysqli_errno( $this->dbh );
    17801829                                } else {
    17811830                                        // $dbh is defined, but isn't a real connection.
    17821831                                        // Something has gone horribly wrong, let's try a reconnect.
    1783                                         $mysql_errno = 2006;
     1832                                        $this->last_err_no = 2006;
    17841833                                }
    17851834                        } else {
    17861835                                if ( is_resource( $this->dbh ) ) {
    1787                                         $mysql_errno = mysql_errno( $this->dbh );
     1836                                        $this->last_err_no = mysql_errno( $this->dbh );
    17881837                                } else {
    1789                                         $mysql_errno = 2006;
     1838                                        $this->last_err_no = 2006;
    17901839                                }
    17911840                        }
    17921841                }
    17931842
    1794                 if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
     1843                if ( empty( $this->dbh ) || 2006 == $this->last_err_no ) {
    17951844                        if ( $this->check_connection() ) {
    17961845                                $this->_do_query( $query );
    17971846                        } else {
     
    17981847                                $this->insert_id = 0;
    17991848                                return false;
    18001849                        }
    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                                }
    18071859                        } 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                                }
    18091866                        }
    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                 }
    18171867
    1818                 if ( $this->last_error ) {
    18191868                        // Clear insert_id on a subsequent failed insert.
    18201869                        if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
    18211870                                $this->insert_id = 0;