PHP mysqli_error() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_error() returns the error message string for the most recent MySQLi function call on a connection.

Object-oriented style reads the $mysqli->error property. An empty string means no error. Use it together with mysqli_errno() when a query returns false.

Syntax

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

PHP
$msg = mysqli_error($mysqli);
CallWhat it does
mysqli_error($mysqli)Returns the last error message, or an empty string.
$mysqli->errorObject-oriented property form.
mysqli_error($mysqli) !== ""True when the connection has a non-empty error string.

For connect failures use mysqli_connect_error(). For multiple errors from one call, see mysqli_error_list(). Prepared statement errors have mysqli_stmt_error().

Library declarations
PHP
mysqli_error(mysqli $mysql): string

Parameters

ParameterDescription
mysqlRequired. The mysqli connection to inspect.

Return Value

Return ValueDescription
stringThe error description, or an empty string when there is no error.

PHP Examples

Example 1 — Empty Error After Success

A valid query leaves error empty.

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

How It Works

  1. SELECT 1 succeeds on the demo database.
  2. mysqli_error() returns an empty string.
  3. var_export shows '' clearly.
  4. The connection is closed.

Example 2 — Read the OO Error Property

Print $mysqli->error after a syntax mistake.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$mysqli->query("SELEC 1");
echo $mysqli->error, PHP_EOL;
$mysqli->close();

How It Works

  1. SELEC is an intentional typo.
  2. The server rejects the statement.
  3. error holds the human-readable explanation.
  4. Sample output shows a syntax error message.

Example 3 — Combine Errno and Error

Print both code and message after a missing table.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_query($mysqli, "SELECT * FROM no_such_table");
echo mysqli_errno($mysqli), " => ", mysqli_error($mysqli), PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. The table name does not exist.
  2. errno provides the numeric code.
  3. error provides the text.
  4. Together they make debugging easier.

Frequently Asked Questions

A string describing the last error on that connection, or an empty string when the last call succeeded.
Read $mysqli->error. It is the same text as mysqli_error($mysqli).
Right after a failing query or other mysqli call, before another call overwrites the message.
connect_error is only for connection attempts. mysqli_error() is for operations on an already open connection.
No. Prepared statements help prevent SQL injection, but you still check return values and error messages when something fails.

Next: mysqli_error_list()

mysqli_error_list() returns a list of errors from the last MySQLi function call as an arra.

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