0

What's the point of MySQL error Codes (like 1054)?

The error messages contain the actual information, so there must be some way of getting some more information out of the error codes.

But what can I use to get anything from the error code alone compared to the error message?

Normally you get both with :

<?PHP
$db_link = new mysqli($hostname, $username, $password, $database);

$statement = $db_link->prepare('SELECT SomeFieldThatDoesNotExist FROM Person');
$statement->execute();

if(!$statement) {
    $specific_error = $db_link->error;
    $error_number = $db_link->errno;
}
?>

Imagine that I have error 1054 (see: https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_bad_field_error).

With just the number 1054, what can I use to get the actual MySQL error message itself?

5
  • There are ways... You can run perror ${errno} and capture its output (should be installed with MySQL Server by default, iirc), or you can read/parse the MySQL source files... it depends on what you're really trying to accomplish. Mar 13, 2016 at 22:39
  • @Michael-sqlbot: Not really. $errno is a constant, not a variable. Jul 12, 2018 at 23:49
  • I was using ${errno} not in the php sense, but rather as a generic variable meaning any arbitrary MySQL error number. Looping through all values of $n from 1 to 3999, run perror ${n} and capture its output. Jul 13, 2018 at 0:04
  • In retrospect, there's a problem with the OP's approach, because many errors have placeholder values. For example, Incorrect key file for table '%s'; try to repair it (error 1034) might not be all that useful, later, since the table name would not be known. Saving only the error number is lossy. Jul 13, 2018 at 0:08
  • @Michael-sqlbot: You are right, I had phrased my question very badly (almost two years ago). I'm correcting with that suggestion and redirecting in the way that I should have been thinking. Jul 13, 2018 at 0:35

2 Answers 2

1

Use the MySQL Error Codes PHP lib on Github: https://github.com/HoldOffHunger/mysql-errors-codes .

Normal error messaging style :

$db_link = new mysqli($this->hostname,$this->username,$this->password);
print($db_link->connect_errno . " : " . $db_link->connect_error);

Normal error messaging output :

13236 : Message: Newly created data directory SOMEDIRECTORY is unusable. You can safely remove it.

New, fuller error-messaging style :

$mysql_error = new MySQLErrorCode();
$error_codes = $mysql_error->ErrorCodes();

print_r($error_codes[13236]);

Output :

13236 : Message: Newly created data directory SOMEDIRECTORY is unusable. You can safely remove it.

'13236' => [
    'error_code' => '13236',
    'internal_code' => 'ER_DATA_DIRECTORY_UNUSABLE',
    'message_template' => 'Message: Newly created data directory %s is unusable. You can safely remove it.',
    'sql_state' => 'HY000',
    'version_information' => 'ER_DATA_DIRECTORY_UNUSABLE was added in 8.0.13.'
],
-1

use SHOW ERRORS;

Manual : https://mariadb.com/kb/en/mariadb/show-errors/

SELECT f();
ERROR 1305 (42000): FUNCTION f does not exist

SHOW COUNT(*) ERRORS;
+-----------------------+
| @@session.error_count |
+-----------------------+
|                     1 |
+-----------------------+

SHOW ERRORS;
+-------+------+---------------------------+
| Level | Code | Message                   |
+-------+------+---------------------------+
| Error | 1305 | FUNCTION f does not exist |
+-------+------+---------------------------+
1
  • My question is more: If you only had "1305" (like, from a log file from years ago), what internal method can you use in mysql to get the rest of the error information? (The level, message, sqlstate, etc..) Mar 13, 2016 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.