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

mysqli::$error

(mysqli_error) Returns the previous error string description
Name:mysqli::$error
Category:MySQLi
Programming Language:php
One-line Description:Returns the last error description of the last function call.

Definition and usage

error / mysqli_error() function returns the last error description (if any) of the last function call.

Example

Example 1 - Object-Oriented Style

Returns the last error description of the last function call (if any):

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

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

// Execute the query and check for errors
if ( ! $mysqli -> query ( "INSERT INTO Persons (FirstName) VALUES ('Glenn')" ) ) {
  echo ( "Error description: " . $mysqli -> error ) ;
}

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Returns the last error description of the last function call (if any):

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

// Execute the query and check for errors
if ( ! mysqli_query ( $con , "INSERT INTO Persons (FirstName) VALUES ('Glenn')" ) ) {
  echo ( "Error description: " . mysqli_error ( $con ) ) ;
}

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