仅从错误代码获取 MySQL 错误消息

Get MySQL Error Message from Only the Error Code

MySQL 错误代码(例如 1054)有什么意义?

错误消息包含实际信息,因此必须有一些方法可以从错误代码中获取更多信息。

但是与错误消息相比,我可以单独从错误代码中获取什么信息?

通常情况下,您可以同时获得:

<?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;
}
?>

假设我有错误 1054(参见:https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_bad_field_error)。

只有数字 1054,我可以使用什么来获取实际的 MySQL 错误消息本身?

使用显示错误;

手动: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 |
+-------+------+---------------------------+

在 Github 上使用 MySQL 错误代码 PHP 库:https://github.com/HoldOffHunger/mysql-errors-codes .

正常错误消息样式:

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

正常错误消息输出:

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

更全面的新错误消息样式:

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

print_r($error_codes[13236]);

输出:

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.'
],