In this article, we will focus on the mysqli_errno() function in PHP, which is used to get the error code associated with the most recent MySQLi operation. We will provide you with an overview of the function, how it works, and examples of its use.

Introduction to the mysqli_errno() function

The mysqli_errno() function is a built-in function in PHP that is used to get the error code associated with the most recent MySQLi operation. This function is useful when you need to handle errors in your MySQLi code and take appropriate action based on the error code.

How to use the mysqli_errno() function

Using the mysqli_errno() function is very simple. You just need to call the function on a valid MySQLi object. Here is an example:

<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");

$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);

if (!$result) {
    $error_code = mysqli_errno($mysqli);
    echo "Failed to execute query. Error code: " . $error_code;
    exit();
}

mysqli_close($mysqli);
?>

In this example, we call the mysqli_connect() function to connect to a MySQL database with a username and password. We then execute a query using the mysqli_query() function and store the result in a variable. We check if there was an error in the query using the !$result condition. If there was an error, we call the mysqli_errno() function to get the error code associated with the most recent MySQLi operation. We then output the error code and exit the script.

Advanced usage

The mysqli_errno() function can also be used in more advanced scenarios. For example, you can use the function to get the error code for a specific MySQLi object. Here is an example:

<?php
$mysqli1 = mysqli_connect("localhost", "username", "password", "database1");
$mysqli2 = mysqli_connect("localhost", "username", "password", "database2");

$query = "SELECT * FROM my_table";
$result1 = mysqli_query($mysqli1, $query);
$result2 = mysqli_query($mysqli2, $query);

if (!$result1) {
    $error_code = mysqli_errno($mysqli1);
    echo "Failed to execute query on connection 1. Error code: " . $error_code;
    exit();
}

if (!$result2) {
    $error_code = mysqli_errno($mysqli2);
    echo "Failed to execute query on connection 2. Error code: " . $error_code;
    exit();
}

mysqli_close($mysqli1);
mysqli_close($mysqli2);
?>

In this example, we create two MySQLi objects and connect to two different MySQL databases with a username and password. We then execute the same query using each connection and store the result in a variable. We check if there was an error in each query using the !$result condition. If there was an error, we call the mysqli_errno() function for the relevant MySQLi object to get the error code associated with the most recent MySQLi operation. We then output the error code and exit the script.

Conclusion

In conclusion, the mysqli_errno() function is a useful tool for handling errors in your MySQLi code and taking appropriate action based on the error code. By understanding how to use the function and its advanced usage scenarios, you can take advantage of this feature to create powerful and flexible MySQLi queries

Practice Your Knowledge

What does the PHP errno function do?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?