connect_errno
In this article, we will focus on the mysqli_connect_errno() function in PHP, which is used to get the last MySQLi connection error code. We will provide you
The mysqli_connect_errno() function returns the error code from the last MySQLi connection attempt, or 0 if no error occurred. It is the standard way to check whether a call to mysqli_connect() (or the new mysqli() constructor) actually succeeded before you run any queries. This page covers its syntax, return value, how it differs from related functions, and the gotchas to watch for.
Why connection checking matters
When a MySQLi connection fails, the $mysqli handle is still returned, but it is not usable. If you skip the check and immediately run a query, PHP emits a warning and your script keeps running with a broken connection — often producing confusing downstream errors. Calling mysqli_connect_errno() right after you connect lets you fail fast with a clear message instead.
A key detail: mysqli_connect_errno() is a standalone function. You call it with no arguments — it reads the global state of the most recent connection attempt, not a specific connection object.
Syntax
mysqli_connect_errno(): int| Parameters | None. |
| Return value | An int: the error code (errno) of the last connection attempt, or 0 if the last attempt succeeded. |
The numeric code corresponds to a MySQL/MariaDB client error number — for example 1045 for access denied or 2002 for connection refused. For the human-readable message, pair it with mysqli_connect_error().
Basic usage (procedural style)
Call the function immediately after mysqli_connect() and exit early if it is non-zero:
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
echo "Connected successfully.";
// execute queries using $mysqli
mysqli_close($mysqli);
?>If the credentials are wrong, this prints something like Failed to connect to MySQL: Access denied for user 'username'@'localhost' and stops. On success it prints Connected successfully. Because the error code is truthy only when non-zero, if (mysqli_connect_errno()) reads naturally as "if an error occurred."
Object-oriented style
When you use the mysqli class, the equivalent of mysqli_connect_errno() is the connect_errno property of the object. Read it the same way — right after construction:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
exit();
}
echo "Connected successfully.";
// execute queries using $mysqli
$mysqli->close();
?>Here $mysqli->connect_errno holds the numeric code and $mysqli->connect_error holds the message — combining them (e.g. (1045) Access denied...) makes log entries easy to grep.
connect_errno vs. related functions
It is easy to confuse the connection-error functions with the general MySQLi error functions. They report on different things:
| Function / property | Reports on | When to use |
|---|---|---|
mysqli_connect_errno() | The last connection attempt | Right after connecting |
mysqli_connect_error() | The last connection attempt (message) | To show a readable connection error |
mysqli_errno() | The last query on an open connection | After mysqli_query() etc. |
mysqli_error() | The last query (message) | To show a readable query error |
In short: use connect_errno/connect_error for the handshake, and errno/error for queries once you are connected.
Common gotchas
- It reflects only the most recent attempt. If you open several connections,
mysqli_connect_errno()reports the last one. For per-object accuracy in OO code, prefer$mysqli->connect_errno. - Check it before the first query, not after. Once you run a query,
mysqli_connect_errno()still reflects the connection attempt, but mixing it up withmysqli_errno()leads to bugs. - A truthy check is enough. You rarely need to compare the code to a specific number;
if (mysqli_connect_errno())covers every failure. - Consider exceptions instead. Since PHP 8.1, MySQLi throws
mysqli_sql_exceptionon errors by default (mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT)), so a failed connection raises an exception rather than returning a bad handle. In that mode you wrap the connection intry/catchinstead of pollingconnect_errno.
Related functions
mysqli_connect()— open a connection.mysqli_connect_error()— message for the last connection error.mysqli_real_connect()— open a connection with extra options.- The MySQLi extension overview — broader context on MySQLi.
Conclusion
mysqli_connect_errno() is the quickest reliable check that a MySQLi connection opened correctly: a non-zero return means the handshake failed, 0 means you are good to query. Pair it with mysqli_connect_error() for readable messages, keep it distinct from the query-level mysqli_errno(), and on PHP 8.1+ consider try/catch with exception mode as a modern alternative.