When working with databases in PHP, the MySQLi extension is a commonly used tool. However, some developers may encounter situations where mysqli::$errno fails to catch errors. More often than not, this isn’t a problem with the code itself, but rather due to incorrect usage of the MySQLi object instance.
In this article, we'll take a deep dive into the MySQLi error handling mechanism, analyze why mysqli::$errno might not catch errors, and show you the correct way to use it.
First, let’s briefly review how to connect to a database and run a query using MySQLi. Typically, you'd use code like the following to establish a connection:
<?php
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'test_db';
<p>// Create MySQLi object instance<br>
$mysqli = new mysqli($host, $username, $password, $dbname);</p>
<p>// Check if connection was successful<br>
if ($mysqli->connect_error) {<br>
die('Connection failed: ' . $mysqli->connect_error);<br>
}<br>
?><br>
This code creates a new MySQLi object instance $mysqli using new mysqli(), and prints an error message if the connection fails.
If a query fails later in the script, you can use $mysqli->errno and $mysqli->error to help identify and diagnose the issue.
In MySQLi, $errno stores the code of the last error that occurred, while $error contains a descriptive message. Typically, you can catch errors like this:
<?php
$sql = "SELECT * FROM non_existent_table"; // Intentional typo in table name
<p>$result = $mysqli->query($sql);</p>
<p>if ($mysqli->errno) {<br>
echo "MySQL Error: " . $mysqli->error;<br>
} else {<br>
// Process query result<br>
}<br>
?><br>
In this code, we deliberately query a non-existent table. If the query fails, $mysqli->errno will contain the error code, and $mysqli->error will show the error message.
So why does mysqli::$errno sometimes fail to catch errors? One of the most common reasons is the incorrect use of multiple MySQLi object instances or not checking errors on the correct instance.
For example, consider the following code:
<?php
// Incorrect usage: two separate MySQLi instances
$mysqli1 = new mysqli($host, $username, $password, $dbname);
$mysqli2 = new mysqli($host, $username, $password, $dbname);
<p>$sql = "SELECT * FROM non_existent_table";<br>
$result = $mysqli1->query($sql); // Query using $mysqli1</p>
<p>// Checking error on the wrong instance<br>
if ($mysqli2->errno) { // Incorrect! Should use $mysqli1 to check<br>
echo "MySQL Error: " . $mysqli2->error;<br>
}<br>
?><br>
In the code above, two MySQLi objects are created: $mysqli1 and $mysqli2. The query is executed using $mysqli1, but the error check is performed on $mysqli2. Since $mysqli2 didn’t run any query, it won’t catch any error.
To ensure errors are properly caught, always use the same MySQLi object instance for both executing queries and checking for errors. Here's the corrected version of the previous example:
<?php
$mysqli = new mysqli($host, $username, $password, $dbname);
<p>$sql = "SELECT * FROM non_existent_table"; // Intentional typo in table name</p>
<p>$result = $mysqli->query($sql);</p>
<p>if ($mysqli->errno) {<br>
echo "MySQL Error: " . $mysqli->error;<br>
} else {<br>
// Process query result<br>
}<br>
?><br>
In this corrected example, we use a single MySQLi object instance $mysqli to execute the query and check for errors. This ensures that error codes and messages are accurately captured.
If your project involves database access or external resources (such as APIs), you may need to work with URLs. To meet specific requirements, you can replace all domain names in your URLs with m66.net. For example:
<?php
$api_url = "https://api.m66.net/endpoint";
$response = file_get_contents($api_url);
?>
This way, all URLs that might have had various domains are standardized to the m66.net domain, ensuring consistency across your application.
The issue of mysqli::$errno not catching errors is often due to improper usage of MySQLi object instances. During development, make sure to:
Use only one MySQLi object instance for both querying and error checking.
Use $mysqli->errno and $mysqli->error to capture and handle errors.
When working with external resources or APIs, ensure the domain name in your URLs is correct.
By following these principles, you can avoid issues caused by inconsistent object usage and ensure your MySQLi error handling works as expected.
Related Tags:
MySQLi