English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_warning_count() function returns the number of warnings in the last query of the connection.
If your last MySQLi function call executed a MySQL query and whether it generated any errors. The function's purpose is: to count the number of errors generated by the last executed query and return the result.
mysqli_warning_count($con)
Serial number | Parameters and descriptions |
---|---|
1 | con (required) This is an object representing the connection to the MySQL Server. |
The PHP mysqli_warning_count() function returns an integer value representing the number of warnings generated during the execution of the last query. If no warnings were generated during the last execution, this function returns0
This function was originally 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"); );
The following examples demonstratemysqli_warning_count()Usage of the function (procedural style)-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Inserting record into employee table $sql = "INSERT IGNORE INTO emp VALUES ("1, 'Sanjay', NULL, DATE('1981-12-05', 2566"); mysqli_query($con, ";"); //Warning count $count = mysqli_warning_count($con); print("Warning count: ",$count,"\n"); $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, ";"); //Warning count $count = mysqli_warning_count($con); print("Warning count: ",$count); //Close connection mysqli_close($con); ?>
Output results
Warning count: 1 Warning count: 2
In the object-oriented style, the syntax of this function is$con-> warning_count, where$conIs a connection object-
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); //Inserting record into employee table $con -> query("INSERT IGNORE into emp values(1, 'Sanjay', NULL, DATE('1981-12-05', 2566); //Warning count $count1 = $con-> warning_count; print("Warning count: ".$count1."\n"); //Inserting record into employee table $con -> query("INSERT IGNORE into emp values(15, 'Swetha', 'Yellapragada', DATE('1990-11-25', 9986), (15, NULL, 'Prayaga', DATE('1990-11-25', 9986); //Warning count $count2 = $con-> warning_count; print("Warning count: ".$count2); //Close connection $con -> close(); ?>
Output results
Warning count: 0 Warning count: 2
The following is the functionmysqli_warning_count()Another example-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Warning count of correct query mysqli_query($con, "SELECT * FROM EMP); print("Warning count (correct query): ".mysqli_warning_count($con)."\n"); //Query to delete unknown table mysqli_query($con, "drop table if exists WrongTable"); print("Warning count: ".mysqli_warning_count($con)."\n"); //Warning before the last statement mysqli_query($con, "INSERT IGNORE into emp values(107, 'Sunitha', NULL, DATE('1981-12-05', 2566); mysqli_query($con, "INSERT IGNORE into emp values(7, 'Mohit', 'Sharma', DATE('1981-12-05', 2566); print("Warning count (if there were errors before the last query): ".mysqli_warning_count($con)."\n"); //Close connection mysqli_close($con); ?>
Output results
Warning count (correct query): 0 Insert ID: (multiple inserts) 6 Insert ID (update query): 0 Warning count (if there were errors before the last query) 0
Returns the number of warnings in the last query of the connection.
<?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); $warnings = mysqli_warning_count($conn); print("Number of warnings in the query:" . $warnings . "\n"); if ($warnings) { if ($result = mysqli_query($conn, "SHOW WARNINGS")) { $row = mysqli_fetch_row($result); printf("%s (%d): %s\n", $row[0], $row[1], $row[2]); mysqli_free_result($result); } } mysqli_close($conn); ?>
Output results
Database connected Number of warnings in the query:1 Warning (1265): Data truncated for column 'Name' at row 1