PHP Function Reference

PHP mysqli $warning_count Property



The PHP mysqli::$warning_count / mysqli_warning_count() function returns the number of warnings from the last query in the connection.

Note: For retrieving warning messages, the SQL command SHOW WARNINGS [limit row_count] can be used.

Syntax

//Object-oriented style
$mysqli->warning_count;

//Procedural style
mysqli_warning_count(mysql)

Parameters

mysql Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init().

Return Value

Returns the number of warnings or zero if there are no warnings.

Example: Object-oriented style

The example below shows the usage of mysqli::$warning_count property.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_warning_count) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

$mysqli->query("CREATE TABLE myEmployee LIKE Employee");

//a remarkable city in Wales
$query = "INSERT INTO myEmployee (Name, Age, City) VALUES('John', 28
        'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch')";

$mysqli->query($query);

if ($mysqli->warning_count) {
  if ($result = $mysqli->query("SHOW WARNINGS")) {
    $row = $result->fetch_row();
    printf("%s, %d, %s \n", $row[0], $row[1], $row[2]);
    $result->close();
  }
}

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

Warning (1264): Data truncated for column 'City' at row 1

Example: Procedural style

The example below shows the usage of mysqli_warning_count() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_warning_count()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

mysqli_query($mysqli, "CREATE TABLE myEmployee LIKE Employee");

//a remarkable city in Wales
$query = "INSERT INTO myEmployee (Name, Age, City) VALUES('John', 28
        'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch')";

mysqli_query($mysqli, $query);

if (mysqli_warning_count($mysqli)) {
  if ($result = mysqli_query($mysqli, "SHOW WARNINGS")) {
    $row = mysqli_fetch_row($result);
    printf("%s, %d, %s \n", $row[0], $row[1], $row[2]);
    mysqli_free_result($result);
  }
}

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

Warning (1264): Data truncated for column 'City' at row 1

❮ PHP MySQLi Reference