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
Comment #1
Steve Simms commentedThis 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.
Comment #2
magico commentedWorkaround: 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?
Comment #3
Anonymous (not verified) commentedResurrecting this old bug. I find that it still exists and will be submitting a patch for it.
Comment #4
dpearcefl commentedDue 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.
Comment #5
dpearcefl commentedGoing to open this issue until I can confirm it doesn't exist in modern Drupal.
Comment #6
Anonymous (not verified) commentedUnassigning.
Comment #7
aspilicious commented5.x isn't supported anymore :)