PHP mysqli_get_warnings() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_get_warnings() returns the first warning from the last MySQL operation as a mysqli_warning object, or false when there are no warnings.

Each warning object holds message, sqlstate and errno. Call next() on the object to walk the rest of the warning list. Many beginners prefer SHOW WARNINGS in SQL or mysqli_warning_count() for a simple count.

Syntax

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

PHP
$w = mysqli_get_warnings($mysqli);
CallWhat it does
mysqli_get_warnings($mysqli)Returns a mysqli_warning object or false if none.
$w = mysqli_get_warnings($mysqli); echo $w->message;Reads the first warning message text.
while ($w && $w->next()) { /* ... */ }Walks later warnings after the first one.

Call mysqli_warning_count() first if you only care whether warnings exist. Official docs also recommend SHOW WARNINGS for many apps. Reading warnings can change what mysqli_affected_rows() reports afterward, so read affected rows first if you need both.

Library declarations
PHP
mysqli_get_warnings(mysqli $mysql): mysqli_warning|false

Parameters

ParameterDescription
mysqlRequired. The mysqli connection that produced the warnings.

Return Value

Return ValueDescription
mysqli_warning|falseThe first warning object, or false when the warning list is empty.

PHP Examples

Example 1 — Check Whether Warnings Exist

After a query, print whether get_warnings found anything. Sample assumes no warnings.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_query($mysqli, "SELECT 1");

$w = mysqli_get_warnings($mysqli);
echo $w === false ? "No warnings." : $w->message, PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. Run a simple SELECT that normally produces no warnings.
  2. mysqli_get_warnings() returns false when the list is empty.
  3. We print a clear message for the false case.
  4. Close the connection at the end.

Example 2 — Read Count Before Details

Use warning_count as a cheap gate before calling get_warnings.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_query($mysqli, "UPDATE products SET price = price");

$count = mysqli_warning_count($mysqli);
echo "count: ", $count, PHP_EOL;

if ($count > 0) {
    $w = mysqli_get_warnings($mysqli);
    echo $w->message, PHP_EOL;
} else {
    echo "Nothing to list.", PHP_EOL;
}

mysqli_close($mysqli);

How It Works

  1. mysqli_warning_count() returns how many warnings the last statement made.
  2. In this sample the count is 0.
  3. We skip get_warnings when there is nothing to read.
  4. This keeps beginner code simple and avoids false checks.

Example 3 — Print First Warning Fields

When a warning exists, show message, errno and sqlstate from the object.

PHP
<?php
// Tutorial sample: pretend get_warnings returned a warning object.
$w = (object) [
    "message"  => "Data truncated for column 'code' at row 1",
    "errno"    => 1265,
    "sqlstate" => "01000",
];

echo $w->message, PHP_EOL;
echo "errno=", $w->errno, " sqlstate=", $w->sqlstate, PHP_EOL;

How It Works

  1. A real mysqli_warning object exposes message, errno and sqlstate.
  2. This example uses a fixed object so the page does not need a live DB.
  3. Print the message first for humans.
  4. Errno and sqlstate help when matching MySQL documentation.

Frequently Asked Questions

It returns the first server warning for the connection as a mysqli_warning object, or false if there are none.
Call next() on the mysqli_warning object to move to the next warning in the list.
Often yes. SHOW WARNINGS is easy to read as a result set. get_warnings() is the object API for the same idea.
It can. If you need affected rows, call mysqli_affected_rows() before mysqli_get_warnings().
mysqli_error() is for hard failures. Warnings are softer notices that did not necessarily stop the query.

Next: mysqli_info()

mysqli_info() returns a short summary string about the last query when MySQL provides one,.

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