PHP mysqli_sqlstate() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_sqlstate() returns the SQLSTATE error code from the last MySQLi operation on the connection as a five-character string.

SQLSTATE is a standardized code (for example 00000 for success-style state, or 42S02 for unknown table). Use it with mysqli_errno(), mysqli_error(), warning_count(), and get_warnings() when diagnosing failures.

Syntax

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

PHP
$state = mysqli_sqlstate($mysql);
CallWhat it does
mysqli_sqlstate($link)Returns a five-character SQLSTATE string for the last operation.
$mysqli->sqlstateObject-oriented property form of the same value.
mysqli_errno($link) . ' / ' . mysqli_error($link)Companion numeric errno and human error text.

In OO style, sqlstate is a property ($mysqli->sqlstate), while procedural code uses mysqli_sqlstate($link). An empty or 00000-style value usually means no error condition to report.

Library declarations
PHP
mysqli_sqlstate(mysqli $mysql): string

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli connection.

Return Value

Return ValueDescription
stringA five-character SQLSTATE code describing the last error/state.

PHP Examples

Example 1 — Read a Success SQLSTATE

Demo of the common no-error code.

PHP
<?php
$sqlstate = '00000'; // typical success-style sample
echo 'sqlstate=', $sqlstate, PHP_EOL;

How It Works

  1. After a successful operation, SQLSTATE is often 00000.
  2. Procedural code calls mysqli_sqlstate($link).
  3. OO code reads $mysqli->sqlstate.
  4. Sample output when connected to a demo database matches this success code.

Example 2 — Pair With errno and error

Print a fixed diagnostic trio for teaching.

PHP
<?php
$errno = 1146;
$error = "Table 'demo.missing' doesn't exist";
$sqlstate = '42S02';
echo "errno={$errno}", PHP_EOL;
echo "sqlstate={$sqlstate}", PHP_EOL;
echo "error={$error}", PHP_EOL;

How It Works

  1. errno is the MySQL error number.
  2. sqlstate is the five-character standard code.
  3. error is the readable message.
  4. Log all three when teaching diagnostics.

Example 3 — Empty Message Still Check State

Always read diagnostics after a false return from query/prepare.

PHP
<?php
$queryOk = false;
if (!$queryOk) {
    echo 'check sqlstate/errno/error next', PHP_EOL;
}

How It Works

  1. A false from query/prepare is your cue.
  2. Call sqlstate() along with error().
  3. warning_count() / get_warnings() cover warnings, not only hard errors.
  4. Fix the SQL or permissions based on the codes.

Frequently Asked Questions

A five-character SQLSTATE string for the last MySQLi operation on that connection.
errno is MySQL’s numeric code; sqlstate is the standardized five-character SQLSTATE value.
Usually you read the $mysqli->sqlstate property. Procedural code uses mysqli_sqlstate($link).
It commonly means no error condition for the last operation.
mysqli_error(), mysqli_errno(), mysqli_warning_count(), and mysqli_get_warnings().

Next: mysqli_ssl_set()

mysqli_ssl_set() configures SSL key and certificate paths on a MySQLi handle before the co.

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