W3docs

errno

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

This article covers the PHP mysqli_errno() function, which returns the numeric error code for the most recent call on a MySQLi connection. You'll learn what it returns, how to read the code, how it differs from mysqli_error(), and how to use it with both the procedural and object-oriented MySQLi styles.

What mysqli_errno() does

mysqli_errno() is a built-in PHP function that returns the error code from the last MySQLi function that ran on a given connection. It's the programmatic counterpart to a human-readable message: where mysqli_error() gives you a sentence like "Table 'db.my_table' doesn't exist", mysqli_errno() gives you the raw integer (here, 1146) that you can compare against in code.

Syntax and return value

mysqli_errno(mysqli $mysqli): int
  • Parameter$mysqli: a connection returned by mysqli_connect() (procedural) or new mysqli(...) (object-oriented).
  • Return value — the error code for the last call on that connection, or 0 if no error occurred. Always check the return value rather than assuming an error is present.

Why a number? Numeric codes are stable and easy to branch on. The text of an error message can change between MySQL versions or locales, but the code (e.g. 1062 for a duplicate key) stays the same — so codes are what you should test against in if/switch logic.

Using mysqli_errno() (procedural style)

It requires a valid MySQLi connection as its only argument. Here is a typical pattern:

How to use the mysqli_errno() function?

<?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);
?>

Here we first verify the connection succeeded, then run a query with mysqli_query(). If the query fails (!$result), mysqli_errno($mysqli) gives us the numeric code so we can log it and exit.

Pairing errno with mysqli_error()

In practice you almost always want both the code (to branch on) and the message (to log or display). Use mysqli_errno() together with mysqli_error():

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

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

if (!$result) {
    $code    = mysqli_errno($mysqli);   // e.g. 1146
    $message = mysqli_error($mysqli);   // e.g. "Table 'database.missing_table' doesn't exist"

    // Branch on the stable numeric code, not the message text.
    if ($code === 1146) {
        echo "The table is missing. ";
    }
    echo "[$code] $message";
}

mysqli_close($mysqli);
?>

Object-oriented style

If you create the connection with new mysqli(...), call $mysqli->errno as a property (and $mysqli->error for the message) instead of the procedural function:

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

if (!$mysqli->query("SELECT * FROM missing_table")) {
    echo "Error " . $mysqli->errno . ": " . $mysqli->error;
}

$mysqli->close();
?>

Both styles report the same code — pick the one that matches the rest of your codebase. See PHP MySQLi for an overview of the extension.

Common MySQL error codes

A few codes you'll meet often when handling MySQLi errors:

CodeMeaning
1045Access denied for user (wrong username/password)
1049Unknown database
1062Duplicate entry for a unique/primary key
1146Table doesn't exist
1264Out-of-range value for a column
1452Foreign key constraint fails

For the full list, see the official MySQL server error reference.

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
$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() reports the error for one specific connection. With multiple connections open, the code is scoped to whichever connection object you pass — so always pass the right one.

Conclusion

mysqli_errno() returns the numeric error code for the most recent operation on a MySQLi connection, or 0 when there was no error. Pair it with mysqli_error() for the message, branch on the stable numeric code, and pass the correct connection object when several are open — that's the foundation of robust MySQLi error handling.

Practice

Practice
What does the PHP mysqli_errno() function return?
What does the PHP mysqli_errno() function return?
Was this page helpful?