db_error() just does return mysql_errno();
mysql_errno() returns error code of the last mysql operation.

Usual use of this could be:

if (db_query($sql) === FALSE) {
  echo 'Error code is '. db_error();
}

db_query() executes _db_query, which in case of failed query does
trigger_error(mysql_error() ."\nquery: ". htmlspecialchars($query), E_USER_ERROR);

But it seems that there is an error handler that saves error messages from trigger_error() into the db.
That means, that after failed query another nysql query is executed (that saves first query's error message to db).

So the db_error() after db_query() does not return correct error code!

Comments

Steve Simms’s picture

Title: db_error() broken » db_error() broken in MySQL

This seems to be a MySQL-specific problem. PostgreSQL's equivalent function is pg_last_error, which returns the last error of the connection, rather than being cleared with each query.

A quick look through the PHP documentation didn't show an equivalent function for MySQL, so fixing this will probably mean storing the error in a variable.

magico’s picture

Workaround: should we save the error in a variable when we save the error in the database, so it can be used in the db_error() function?

Anonymous’s picture

Version: x.y.z » 5.x-dev
Assigned: Unassigned »

Resurrecting this old bug. I find that it still exists and will be submitting a patch for it.

dpearcefl’s picture

Status: Active » Closed (won't fix)

Due to the age of the last comment on this issue and due to the fact that D5 is no longer supported, I am closing this issue.

dpearcefl’s picture

Status: Closed (won't fix) » Postponed

Going to open this issue until I can confirm it doesn't exist in modern Drupal.

Anonymous’s picture

Assigned: » Unassigned

Unassigning.

aspilicious’s picture

Status: Postponed » Closed (won't fix)

5.x isn't supported anymore :)