Current Location: Home> Function Categories> mysqli::$warning_count

mysqli::$warning_count

(mysqli_warning_count) Returns the number of warnings for the last query of the given link
Name:mysqli::$warning_count
Category:MySQLi
Programming Language:php
One-line Description:Returns the number of warnings last query in the connection.

Definition and usage

warning_count / mysqli_warning_count() function returns the number of warnings for the last query.

Example

Example 1 - Object-Oriented Style

Returns the number of warnings last query:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

// Check the connection
if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

$mysqli -> query ( "CREATE TABLE myPersons LIKE Persons" ) ;

$sql = "INSERT INTO myPersons (FirstName) VALUES(
'Hdghfhjgjtjyjnjkghfjbmbngfgffdhfhjgjgkjhlkhlkjljljlkjkljkljkljlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkj')" ;

$mysqli -> query ( $sql ) ;

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 ( ) ;
  }
}

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Returns the number of warnings last query:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

mysqli_query ( $con , "CREATE TABLE myPersons LIKE Persons" ) ;

$sql = "INSERT INTO myPersons (FirstName) VALUES(
'Hdghfhjgjtjyjn.,,??l?jkghfjbmbngfgffdhfhjgjgkjhlkhlkjljljlkjkljkljkljkljlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkj')" ;

mysqli_query ( $con , $sql ) ;

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

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles