PHP mysql_error() Function

Definition and Usage

The mysql_error() function returns the text error information generated by the last MySQL operation.

This function returns the error text of the last MySQL function, or returns '' (an empty string) if no error occurs.

Syntax

mysql_error();connection)
Parameter Description
connection Optional. Specifies the SQL connection identifier. If not specified, it uses the last opened connection.

Example

In this example, we will try to log in to a MySQL server using the wrong username and password:

<?php
$con = mysql_connect("localhost","wrong_user","wrong_pwd");
if (!$con)
  {
  die();mysql_error(););
  }
mysql_close($con);
?>

Output similar to:

Access denied for user 'wrong_user'@'localhost'
(using password: YES)