PHP mysqli_errno() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_errno() returns the error code for the most recent function call on a MySQLi connection.

Object-oriented style reads the $mysqli->errno property. 0 means the last operation succeeded. Pair it with mysqli_error() for the message text.

Syntax

Call mysqli_errno() with the values you need. Start with this line.

PHP
$code = mysqli_errno($mysqli);
CallWhat it does
mysqli_errno($mysqli)Returns 0 on success or a MySQL error code.
$mysqli->errnoObject-oriented property form.
mysqli_errno($mysqli) === 0True when the last call on that connection did not set an error.

This is for an already open connection. Connection failures use mysqli_connect_errno() instead. Statement-level errors can also be read from mysqli_stmt_errno().

Library declarations
PHP
mysqli_errno(mysqli $mysql): int

Parameters

ParameterDescription
mysqlRequired. The mysqli connection to inspect.

Return Value

Return ValueDescription
int0 when there is no error; otherwise a MySQL error code.

PHP Examples

Example 1 — Errno After a Good Query

Run SELECT 1 and show errno 0. Sample output when connected to a demo database.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_query($mysqli, "SELECT 1");
echo mysqli_errno($mysqli), PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. Connect to the demo database.
  2. A valid SELECT runs.
  3. mysqli_errno() returns 0.
  4. Close the connection.

Example 2 — Errno After a Bad Table Name

Trigger error 1146 and print it with the OO property.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$mysqli->query("SELECT * FROM missing_table");
echo $mysqli->errno, PHP_EOL;
$mysqli->close();

How It Works

  1. query() references a table that does not exist.
  2. MySQL sets error code 1146.
  3. $mysqli->errno exposes that code.
  4. Sample output prints 1146.

Example 3 — Branch on Errno

Choose a beginner-friendly message when errno is not zero.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_query($mysqli, "SELECT * FROM missing_table");

if (mysqli_errno($mysqli) !== 0) {
    echo "Query failed (", mysqli_errno($mysqli), ")", PHP_EOL;
} else {
    echo "Query ok", PHP_EOL;
}

mysqli_close($mysqli);

How It Works

  1. A bad query sets a non-zero errno.
  2. The if condition detects failure.
  3. The code is included in the message.
  4. In real apps also log mysqli_error().

Frequently Asked Questions

It returns the integer MySQL error code for the last operation on that connection, or 0 when there was no error.
Read $mysqli->errno. It matches mysqli_errno($mysqli).
connect_errno is only about establishing the connection. errno is for later calls such as query() on an open link.
Check both. A failed query often returns false, and errno/error explain why.
MySQL error documentation lists codes such as 1062 for duplicate key and 1146 for unknown table. The error string helps too.

Next: mysqli_error()

mysqli_error() returns the error message string for the most recent MySQLi function call o.

mysqli_error() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful