PHP mysqli_warning_count() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_warning_count() returns how many warnings the last MySQLi operation produced on the connection.

Warnings are softer than errors — the statement may still succeed while something looks suspicious (truncated data, unsafe settings, and similar). Pair this count with mysqli_get_warnings() to read each warning, and with sqlstate()/error() for hard failures.

Syntax

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

PHP
$n = mysqli_warning_count($mysql);
CallWhat it does
mysqli_warning_count($link)Returns the number of warnings from the last operation.
$mysqli->warning_countObject-oriented property form.
mysqli_get_warnings($link)Fetches the warning list when the count is greater than zero.

A zero count means no warnings for the last command. OO style usually reads the warning_count property. Checking warnings is part of careful diagnostics alongside errno, error, and sqlstate.

Library declarations
PHP
mysqli_warning_count(mysqli $mysql): int

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli connection.

Return Value

Return ValueDescription
intThe number of warnings generated by the previous operation (0 if none).

PHP Examples

Example 1 — Read a Zero Warning Count

Happy-path demo after a clean statement.

PHP
<?php
$count = 0; // stand-in for mysqli_warning_count($link)
echo 'warning_count=', $count, PHP_EOL;

How It Works

  1. Run a statement on the connection.
  2. Call mysqli_warning_count($link).
  3. Zero means no warnings were reported.
  4. Sample output when connected to a demo database is often 0 for clean SQL.

Example 2 — Non-Zero Count Then get_warnings

Show the diagnostic follow-up when warnings exist.

PHP
<?php
$count = 2;
echo 'warning_count=', $count, PHP_EOL;
if ($count > 0) {
    echo 'next: mysqli_get_warnings($link)', PHP_EOL;
}

How It Works

  1. A positive count means warnings are waiting.
  2. Call get_warnings() to inspect errno/sqlstate/message items.
  3. Fix the SQL, types, or charset that caused them.
  4. Hard errors still use error()/errno()/sqlstate() separately.

Example 3 — OO Property Form

Object style reads warning_count like other diagnostic properties.

PHP
<?php
class DemoMysqli {
    public $warning_count = 1;
}
$mysqli = new DemoMysqli();
echo $mysqli->warning_count, PHP_EOL;

How It Works

  1. OO code often uses $mysqli->warning_count.
  2. Procedural code calls mysqli_warning_count($link).
  3. Both report the same integer count.
  4. Combine with get_warnings() when the count is not zero.

Frequently Asked Questions

An integer count of warnings from the last MySQLi operation on that connection.
No. Warnings can appear even when the statement largely succeeded. Errors are harder failures.
Call mysqli_get_warnings() when warning_count is greater than zero.
mysqli_sqlstate(), mysqli_errno(), mysqli_error(), and mysqli_get_warnings().
Usually you read the $mysqli->warning_count property; procedural code uses the function form.

Back to MySQLi Functions

Browse all PHP MySQLi function tutorials.

MySQLi functions →

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