PHP mysqli_connect_error() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

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

Object-oriented style reads the $mysqli->connect_error property. The procedural function takes no arguments. On a successful connect it returns null (or an empty message depending on context), and on failure it returns text such as Access denied...

Syntax

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

PHP
$msg = mysqli_connect_error();
CallWhat it does
mysqli_connect_error()Returns the last connect error message, or null when none.
$mysqli->connect_errorObject-oriented property form.
mysqli_connect_error() ?? "none"Uses null coalescing when there is no error string.

Use with mysqli_connect_errno(). For errors after a good connect, use mysqli_error() / $mysqli->error instead.

Library declarations
PHP
mysqli_connect_error(): ?string

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
?stringThe connect error message, or null when the last attempt succeeded.

PHP Examples

Example 1 — No Error on a Good Connect

Print the connect error value after a successful demo connect.

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

How It Works

  1. The demo connection succeeds.
  2. mysqli_connect_error() has nothing to report.
  3. var_export prints NULL.
  4. The connection is closed.

Example 2 — Object Property Message

Read connect_error from the mysqli object after a bad password.

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

How It Works

  1. new mysqli() is called with a wrong password.
  2. connect_errno is non-zero on the object.
  3. connect_error holds the readable message.
  4. Sample output shows a typical access-denied string.

Example 3 — Pair Errno With Error Text

Log both the code and the message for easier debugging.

PHP
<?php
$mysqli = @mysqli_connect("localhost", "demo", "bad-password", "shop");
if (!$mysqli) {
    echo "[", mysqli_connect_errno(), "] ", mysqli_connect_error(), PHP_EOL;
}

How It Works

  1. Connect is attempted with bad credentials.
  2. The false return triggers the error branch.
  3. errno and error are printed together.
  4. This format is handy in development logs.

Frequently Asked Questions

A string describing the last connection failure, or null when the last connect attempt worked.
Read $mysqli->connect_error after new mysqli(...). It mirrors the procedural function.
Because connect_error is only about connecting. Use mysqli_error($mysqli) for SQL errors on an open link.
Not in production. Log it for yourself and show users a generic "could not connect" message.
No. The procedural function takes no parameters. OO style uses the property on the connection object.

Next: mysqli_current_field()

mysqli_current_field refers to the current field cursor on a mysqli_result: read $result->.

mysqli_current_field() 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