簡體   English   中英

僅從錯誤代碼獲取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/zh/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.'
],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM