PHP mysqli_connect_errno() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_connect_errno() returns the error code from the last connection attempt.

In object-oriented style you read the $mysqli->connect_errno property on the connection object. The procedural function takes no arguments and reports the most recent connect failure code, or 0 when the last attempt succeeded.

Syntax

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

PHP
$code = mysqli_connect_errno();
CallWhat it does
mysqli_connect_errno()Returns 0 on success, or a MySQL error code on failure.
$mysqli->connect_errnoObject-oriented property on the mysqli instance.
mysqli_connect_errno() !== 0True when the last connect attempt failed.

Pair this with mysqli_connect_error() for the message text. For errors after a successful connect, use mysqli_errno() / $mysqli->errno instead.

Library declarations
PHP
mysqli_connect_errno(): int

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
int0 when the last connection attempt succeeded; otherwise a MySQL error code such as 1045.

PHP Examples

Example 1 — Check Connect Errno After Connect

Print 0 when the demo connection works.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
echo "connect_errno=", mysqli_connect_errno(), PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. mysqli_connect() succeeds with demo credentials.
  2. mysqli_connect_errno() returns 0.
  3. Sample output when connected to a demo database shows success.
  4. The connection is closed afterward.

Example 2 — Object Property Form

Read connect_errno from the mysqli object.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
echo "errno=", $mysqli->connect_errno, PHP_EOL;
if ($mysqli->connect_errno === 0) {
    echo "ready", PHP_EOL;
}
$mysqli->close();

How It Works

  1. new mysqli() creates the object even when checking errors.
  2. connect_errno is read as a property.
  3. 0 means the handshake succeeded.
  4. The script prints ready only on success.

Example 3 — Failed Login Code

Show a typical access-denied errno.

PHP
<?php
$mysqli = @mysqli_connect("localhost", "demo", "bad-password", "shop");
echo mysqli_connect_errno(), PHP_EOL;

How It Works

  1. A bad password forces a connect failure.
  2. mysqli_connect() returns false.
  3. mysqli_connect_errno() still reports the code.
  4. 1045 is the common access-denied code in sample output.

Frequently Asked Questions

An integer error code from the last connection attempt. 0 means success.
No. The procedural function has an empty parameter list. In OO style you read $mysqli->connect_errno on the object returned by new mysqli().
connect_errno is only about connecting. mysqli_errno() reports errors from later operations on an open connection.
1045 is the usual Access denied code. Always also read the error string for details.
Many apps enable MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT so connect failures throw. You can still read connect_errno when you catch or when exceptions are off.

Next: mysqli_connect_error()

mysqli_connect_error() returns the error message from the last MySQLi connection attempt.

mysqli_connect_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