mysql_error()

Returns a text error message upon failure of the previous MySQL operation.

 

In this example, a wrong value is used to provide the error message:

 

<?php
	include("/var/www/dbConfig.php");

	$passwd = 'wrong' ; //using a bad value to provide error message

	$connection = mysql_connect( $server, $user, $passwd ) ;

	if( ! $connection ) {

		die('Error connecting to the database! <br>' . mysql_error() ) ;
	}

	echo 'Connected to the ' . $server . ' database server <br>';

	/* MORE CODE TO BE INSERTED HERE */

	mysql_close( $connection ) ;
?>

Displays:

Error connecting to the database!
Access denied for user 'root'@'localhost' (using password: YES)

 

 

*Note: the die() function prints an error message (required) and exits the script. Synonym of the exit() function.

Leave a Reply