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错误信息呢?
答案 0 :(得分:0)
使用SHOW ERRORS;
手动: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 :(得分:0)
在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.'
],