In PHP, you may encounter some common errors when using the mysqli extension to interact with a MySQL database. For these errors, we can get the error code through mysqli::$errno to help us diagnose the problem. In particular, it is crucial to distinguish between database connection errors and SQL syntax errors. This article will explain how to use mysqli::$errno to distinguish these two errors.
Database connection errors usually occur when trying to connect to the database. If the database server has an error in the address, username, password, or database name, the connection will fail. In this case, mysqli::$errno will return a specific error code, and we can judge the problem based on this code.
<?php
$host = 'localhost'; // Database server address
$user = 'root'; // Database username
$password = ''; // Database Password
$database = 'test'; // Database name
// Create a database connection
$conn = new mysqli($host, $user, $password, $database);
// Check if the connection is successful
if ($conn->connect_errno) {
echo "Connection error: " . $conn->connect_error;
// according toerrnoDetermine the error type
if ($conn->connect_errno == 1049) {
echo "The database does not exist!";
} elseif ($conn->connect_errno == 1045) {
echo "Incorrect username or password!";
}
exit();
}
echo "Connect to the database successfully!";
?>
In this example, if the database connection fails, $conn->connect_errno will return an error code. For example, error code 1049 indicates that the database does not exist, and error code 1045 indicates that the user name or password is wrong. With these error codes, you can locate the problem more accurately.
When you execute SQL queries, SQL syntax errors may cause the query to fail. At this time, mysqli::$errno will return code related to SQL errors. By judging the error code, you can quickly distinguish whether it is an SQL syntax error or other types of errors.
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'test';
// Create a database connection
$conn = new mysqli($host, $user, $password, $database);
// Execute a syntax errorSQLQuery
$sql = "SELEC * FROM users"; // mistake的Query:SELEC Should be SELECT
$result = $conn->query($sql);
// examineSQLWhether the execution is successful
if ($result === false) {
echo "SQLmistake: " . $conn->error;
// 判断mistake代码
if ($conn->errno == 1064) {
echo "SQL语法mistake!";
}
exit();
}
echo "Query成功!";
?>
In this example, the SQL query has a syntax error ( SELEC should be SELECT ), and $conn->errno returns 1064, indicating an SQL syntax error. By judging the value of mysqli::$errno , you can quickly locate problems in SQL queries.
mysqli::$errno provides an effective way to distinguish database connection errors from SQL syntax errors. By checking the returned error code, you can quickly diagnose the problem and reduce debugging time.
Database connection error : Connect_errno is usually used to check whether the connection is successful and distinguish different connection errors by error code (e.g., username or password error, database does not exist, etc.).
SQL syntax error : When there is a problem with SQL query, the error code returned by $conn->errno can help you recognize syntax errors.
These two error handling methods can help you better manage and debug database operations and improve the robustness of your code.