Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve last error handling: Record the last_error closer to the queries so it's not lost and don't run 'SELECT FOUND_ROWS()' if the query has failed #156

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading