How to combine mysqli::$errno and language package to implement localized database error prompts?
Error handling of database operations is essential when developing web applications. In order to improve user experience, it is often necessary to display friendly error messages based on different locales. PHP provides mysqli extension to interact with MySQL database, and the mysqli::$errno property can help us obtain error codes, thereby returning the corresponding error information based on the error code.
This article will introduce how to combine mysqli::$errno and language package to implement localization database error prompts.
First, PHP's mysqli extension provides a mysqli::$errno property, which stores the error code for the most recent MySQL error. If we can find the corresponding localized error prompts based on the error code, our application can be more usable and can return appropriate error messages according to different locales.
When implementing it, we usually need the following steps:
Capture database errors and get error code mysqli::$errno .
Get error information for the corresponding language through the language pack.
Display localization error message to the user.
First, we need to create a language package. Suppose we have two languages: English and Chinese. We can store this information in a PHP array to facilitate subsequent searches and references.
// en.php (English language pack)
return [
'1045' => 'Access denied for user',
'1146' => 'Table doesn\'t exist',
'2002' => 'Connection refused',
// More error codes and corresponding error messages can be added
];
// zh.php (Chinese language pack)
return [
'1045' => 'User access denied',
'1146' => 'The table does not exist',
'2002' => 'Connection refused',
// More error codes and corresponding error messages can be added
];
The above are the two language packs we assumed (English and Chinese). In actual applications, you may have more language packages, or use JSON, XML and other formats to store language data.
When a database error occurs, we can get the error code through mysqli::$errno , and then obtain the corresponding error prompt information from the language package based on the error code.
<?php
// Loading language packs
$lang = 'zh'; // Here you can dynamically select according to the user's language settings
$langFile = $lang . '.php';
$errorMessages = include($langFile);
// create MySQLi connect
$mysqli = new mysqli("localhost", "username", "password", "database");
// 检查connect
if ($mysqli->connect_error) {
$errno = $mysqli->connect_errno; // 获取connect错误码
echo '数据库connect失败: ' . $errorMessages[$errno] ?? 'Unknown error';
exit();
}
// Perform query operations
$result = $mysqli->query("SELECT * FROM non_existing_table");
if (!$result) {
// Get the error code and output localized error information
$errno = $mysqli->errno;
echo 'Query failed: ' . $errorMessages[$errno] ?? 'Unknown error';
}
?>
In this example, when the database connection or query operation fails, mysqli::$errno will return the corresponding error code, and then we obtain and display the corresponding error message through the language package.
Sometimes, there may be cases where there is no clear error code corresponding to localized messages. In this case, we can provide a default error message for each error code and further customize it according to the specific situation.
<?php
// Default error message
$defaultMessages = [
'1045' => 'Access denied for user',
'1146' => 'Table doesn\'t exist',
'2002' => 'Connection refused',
];
// Get localized error message,If not,Use the default message
$errorMessage = $errorMessages[$errno] ?? $defaultMessages[$errno] ?? 'An unexpected error occurred.';
// Output error message
echo 'Database error: ' . $errorMessage;
?>
In this way, no matter which error it is, we can ensure that we provide a suitable error prompt to avoid displaying the original information of the database error and causing trouble to the user.
Combining mysqli::$errno and language packages to achieve localized database error prompts is an effective means to improve user experience and international support. You can implement this function in your application through the following steps:
Capture mysqli::$errno error code.
Create a language package and map error codes to localized error prompts.
According to the current user's locale environment, the corresponding language pack is loaded dynamically.
This method ensures that your application always provides clear and easy-to-understand error information when facing users in different languages.