English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_get_warnings() Function Usage and Example

    PHP MySQLi Reference Manual

The mysqli_get_warnings() function returns the errors generated by the last executed query.

Definition and Usage

If your last MySQLi function call executes a MySQL query and generates any errors.mysqli_get_warnings()The function returns an array of errors generated by the last executed query.

Syntax

mysqli_get_warnings($con)

Parameter

NumberParameters and Description
1

con (required)

This is an object representing the connection to the MySQL Server.

Return Value

The PHP mysqli_get_warnings() function returns an array containing the warnings generated during the execution of the last query.

PHP Version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Suppose we have created a table named Emp as follows:

CREATE TABLE EMP(
   ID TINYINT,
   First_Name VARCHAR(50) NOT NULL, 
   Last_Name VARCHAR(10) NOT NULL, 
   Date_Of_Birth date, 
   Salary Int(255)
);

Online example

The following examples demonstratemysqli_get_warnings()Function Usage (Procedural Style)-

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Insert record into employee table
   $sql = "INSERT IGNORE into emp values(1, 'Sanjay', NULL, DATE('1981-12-05'), 2566);
   mysqli_query($con, $sql);
   //Warnings
   $warnings = mysqli_get_warnings($con);
   print("Warning(s): "."
");
   print_r($warnings);
   $sql = "INSERT IGNORE into emp values (15, 'Swetha', 'Yellapragada', DATE('1990-11-25'), 9986), (15, NULL, 'Prayaga', DATE('1990-11-25'), 9986);
   mysqli_query($con, $sql);
   //Warnings
   $warnings = mysqli_get_warnings($con);
   print("\n"."Warning(s): ");
   print_r($warnings);
   //Close connection
   mysqli_close($con);
?>

Output results

Warning(s):
mysqli_warning Object
(
    [message] => Column 'Last_Name' cannot be null
    [sqlstate] => HY000
    [errno] => 1048
)
Warning(s): mysqli_warning Object
(
    [message] => Data truncated for column 'Last_Name' at row 1
    [sqlstate] => HY000
    [errno] => 1265
)

Online example

The following is the functionmysqli_get_warnings()Another example-

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Delete unknown table
   mysqli_query($con, "drop table if exists WrongTable");
   print("\n"."Warning(s): "."\n");
   print_r(mysqli_get_warnings($con));
   //Close connection
   mysqli_close($con);
?>

Output results

Warning(s):
mysqli_warning Object
(
    [message] => Unknown table 'mydb.wrongtable'
    [sqlstate] => HY000
    [errno] => 1051
)

Online example

You can also use the mysqli_warning classnext()Function one by one retrieves warnings as follows:

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   //Insert record into employee table
   $con -> query("INSERT IGNORE into emp values(105, NULL, 'Yellapragada', DATE('1981-12-05'), 2566))";
   if($con-> warning_count){
      $w = mysqli_get_warnings($con);
      do {
         echo "Warning: $w-> errno: $w-> message\n";
      } while ($w-> next());
   }
   //Close connection
   $con -> close();
?>

Output results

Warning: 1048Column 'First_Name' cannot be null
Warning: 1265Data truncated for column 'Last_Name' at row 1

Online example

Return the error produced by the last executed query

<?php
   $servername = "localhost";
   $username = "root";
   $password = "password";
   $dbname = "mydb";
   $conn = new mysqli($servername, $username, $password, $dbname);
   if (!$conn->real_connect($servername, $username, $password, $dbname)) {
      die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
   }
   print("Database connected")."\n";
   mysqli_query($conn, "CREATE TABLE sample (ID INT, Name VARCHAR(20));
   $query = "INSERT IGNORE INTO sample (id,name) VALUES(
      1'Rajesh Ramayan Kootrapally')";
   mysqli_query($conn, $query);
   $count = mysqli_warning_count($conn);
   print("No. Of warnings in the query:").$count."\n";
   if ($count) {
      $warnings = mysqli_get_warnings($conn);
	  print_r($warnings);
   }
   mysqli_close($conn);
   
?>

Output results

Database connected
No. Of warnings in the query:1
mysqli_warning Object
(
    [message] => Data truncated for column 'Name' at row 1
    [sqlstate] => HY000
    [errno] => 1265
)

PHP MySQLi Reference Manual