Skip to content

errno

In this article, we will focus on the mysqli_errno() function in PHP, which returns the numeric error code for the most recent MySQLi operation. We will provide an overview of the function, how it works, and practical examples.

Introduction to the mysqli_errno() function

The mysqli_errno() function is a built-in PHP function that returns the numeric error code for the most recent MySQLi operation. It is useful when you need to handle errors in your MySQLi code and take appropriate action based on the returned code.

How to use the mysqli_errno() function

Using the mysqli_errno() function is straightforward. It requires a valid MySQLi connection object as its parameter. Here is an example:

How to use the mysqli_errno() function?

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

if (!$mysqli) {
    die("Connection failed: " . mysqli_connect_error());
}

$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 first verify that the connection succeeded. 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 mysqli_errno($mysqli) to get the numeric 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. When working with multiple connections, you must pass the specific MySQLi object to the function to retrieve the error code for that particular connection. Here is an example:

Advanced usage of PHP mysqli_errno()

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

if (!$mysqli1 || !$mysqli2) {
    die("One or more connections failed.");
}

$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. 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 mysqli_errno() for the relevant MySQLi object to get the error code associated with that specific connection. We then output the error code and exit the script.

Note: mysqli_errno() returns only a numeric code. To get a human-readable error message, use the companion function mysqli_error($mysqli). Common MySQL error codes include 1045 (Access denied) and 1049 (Unknown database). Refer to the official MySQL error code documentation for a complete list.

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 returned code. By verifying connections, passing the correct connection object, and interpreting the numeric codes, you can create more robust and flexible MySQLi queries.

Practice

What does the PHP errno function do?

Dual-run preview — compare with live Symfony routes.