Database interaction is inevitable when developing web applications. As a popular server-side language, PHP provides multiple ways to operate databases, and MySQLi (MySQL Improved) is one of them. It supports not only an object-oriented approach, but also a process-based approach. MySQLi provides a variety of ways to handle database queries and errors, one of which is mysqli::$errno .
mysqli::$errno is a property in PHP's built-in class mysqli , which is used to return the error code when an error occurs in a database connection. This error code is returned by the MySQL server and is used to identify different types of errors. mysqli::$errno is used in conjunction with the mysqli::$error attribute. The former returns an error code, while the latter returns a specific error message.
With mysqli::$errno , developers can obtain precise error codes, thereby better debugging and handling problems in database operations.
To better understand how to use mysqli::$errno , let's look at a simple example:
<?php
// Create a database connection
$mysqli = new mysqli("localhost", "root", "password", "test_db");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Execute an incorrect query(Table name error)
$query = "SELECT * FROM non_existent_table";
$result = $mysqli->query($query);
// Check whether the query is successful
if (!$result) {
echo "Query failed,Error code: " . $mysqli->errno . "<br>";
echo "error message: " . $mysqli->error;
}
// Close the database connection
$mysqli->close();
?>
In the above code:
We first created a MySQLi connection object $mysqli and checked whether the connection was successful.
Then we execute a query that made a deliberate error, querying for a non-existent_table table that does not exist.
Get the code for querying errors through $mysqli->errno , and get detailed error information through $mysqli->error .
Finally close the database connection.
If you execute this code, the output results are similar to:
Query failed,Error code: 1146
error message: Table 'test_db.non_existent_table' doesn't exist
Here, $mysqli->errno returns error code 1146 , which represents the error type of MySQL: "Table does not exist" .
Here are some common MySQL error codes and their meanings:
1045 : Access denied (incorrect username or password)
1146 : The table does not exist
1064 : SQL syntax error
1054 : Unknown column
1049 : The database does not exist
2002 : Unable to connect to the database server
These error codes can help developers locate problems faster and take corresponding solutions.
In actual development, it is sometimes necessary to dynamically interact with different URLs or database servers in the application. Suppose we have a function that is to perform database operations based on the URL, and we need to obtain the error code through mysqli::$errno . The following is a simple application scenario, assuming that the database link we operate needs to be connected based on different URLs:
<?php
// Setting targetURLThe domain name ism66.net
$url = "https://m66.net/db_connect";
$mysqli = new mysqli("m66.net", "user", "password", "test_db");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Execute a query
$query = "SELECT * FROM users";
$result = $mysqli->query($query);
// Check whether the query is successful
if (!$result) {
echo "Query failed,Error code: " . $mysqli->errno . "<br>";
echo "error message: " . $mysqli->error;
}
// Close the database connection
$mysqli->close();
?>
In this example, we change the domain name of the database connection to m66.net , which means that all database operations are based on this domain name. Regardless of the server location of the database, this method can simplify the configuration of database interaction.
mysqli::$errno is a very useful property in the PHP MySQLi extension, which can help developers get error codes when database errors occur. In actual PHP development, we can use it to perform more precise error handling. Combining MySQL error code, developers can clearly understand the cause of the problem and take corresponding measures to fix it. In addition, mysqli::$errno can also be used as an effective tool to help debug database connection issues when it comes to interacting with external URLs.
Hopefully this article can help you better understand and use mysqli::$errno to effectively handle database errors when developing PHP applications.