Databases Reference
In-Depth Information
// Custom error handler function
function showerror($connection)
{
// Was there an error during connection?
if(mysqli_connect_errno())
// Yes; display information about the connection error
die("Error " .
mysqli_connect_errno($connection) . " : ".
mysqli_connect_error($connection));
else
// No; display the error information for the active connection
die("Error " .
mysqli_errno($connection) . " : ".
mysqli_error($connection));
}
You could then call the showerror( ) function whenever you encounter a database
error:
if(!($connection= @ mysqli_connect
($DB_hostname, $DB_username, $DB_password, $DB_databasename)))
showerror($connection);
This would display a message such as:
Error 1146 : Table 'music.art' doesn't exist
Consider the following code fragment that uses the MySQLi error functions.
// Connect to the MySQL server
$connection = @ mysqli_connect("localhost", "root",
" the_mysql_root_password ", "vapor");
if (mysqli_connect_errno() != 0)
die("Connection problem: " .
mysqli_connect_error() . " (" .
mysqli_connect_errno() . ")");
$result = @ mysqli_query($connection, "SELECT * FROM artis");
if (mysqli_errno($connection) != 0)
die("Query problem: " .
mysqli_error($connection) . " (" .
mysqli_errno($connection) . ")");
If the mysqli_connect statement fails—as it will in this example, because the database
vapor doesn't exist—then mysqli_connect_errno( ) reports a nonzero value and you
see:
Connection problem: Unknown database 'vapor' (1049)
If the database did exist, the mysqli_query( ) statement would fail because the table
artis doesn't exist, and the code would report:
Query problem: Table 'vapor.artis' doesn't exist (1146)
 
Search WWH ::




Custom Search