Skip to content

Commit

Permalink
Improve last error handling: Record the last_error closer to the quer…
Browse files Browse the repository at this point in the history
…ies so it's not lost and don't run 'SELECT FOUND_ROWS()' if the query has failed

Sync https://plugins.trac.wordpress.org/changeset/3123906

Co-Authored-By: vnsavage <1204149+vnsavage@users.noreply.github.com>
  • Loading branch information
dd32 and vnsavage committed Jul 24, 2024
1 parent d8f68ef commit 978fbf9
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions db.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,8 @@ public function query( $query ) {
$this->ex_mysql_query( $statement_before_query, $this->dbh );
}

$this->result = $this->ex_mysql_query( $query, $this->dbh );
$this->result = $this->ex_mysql_query( $query, $this->dbh );
$this->last_error = $this->ex_mysql_error( $this->dbh );

if ( $statement_after_query ) {
$query_for_log = "$query_for_log; $statement_after_query";
Expand All @@ -969,11 +970,12 @@ public function query( $query ) {
$elapsed = $this->timer_stop();
++$this->num_queries;

if ( preg_match( '/^\s*SELECT\s+SQL_CALC_FOUND_ROWS\s/i', $query ) ) {
if ( preg_match( '/^\s*SELECT\s+SQL_CALC_FOUND_ROWS\s/i', $query ) && false !== $this->result ) {
if ( false === strpos( $query, 'NO_SELECT_FOUND_ROWS' ) ) {
$this->timer_start();
$this->last_found_rows_result = $this->ex_mysql_query( 'SELECT FOUND_ROWS()', $this->dbh );
$elapsed += $this->timer_stop();
$this->last_error = $this->ex_mysql_error( $this->dbh );
++$this->num_queries;
$query .= '; SELECT FOUND_ROWS()';
}
Expand All @@ -996,8 +998,6 @@ public function query( $query ) {
}
}

$this->last_error = $this->ex_mysql_error( $this->dbh );

if ( $this->last_error ) {
$this->last_errno = $this->ex_mysql_errno( $this->dbh );
$this->dbhname_heartbeats[ $this->dbhname ]['last_errno'] = $this->last_errno;
Expand Down

0 comments on commit 978fbf9

Please sign in to comment.