Jump to content

[SOLVED] Displaying custom error on connection failure to database


dprichard

Recommended Posts

I am setting up a connection to a mysql database.  I am trying to display a custom error message if something goes wrong with the connection instead of say something like this.

 

PHP Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ttracker'@'localhost' (using password: YES) in C:\DATA\ttracker.dsolutionsgroup.com\htdocs\Connections\time_tracker_connection.php on line 2 PHP Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\DATA\ttracker.dsolutionsgroup.com\htdocs\Connections\time_tracker_connection.php on line 2

 

I purposely put in the wrong password here to see what came up.  This is my code and I was trying to figure out how to say something more normal sounding than displaying the typical error messages if there is a problem with the connection.

 

<?php
mysql_connect("localhost", "username", "password") or die(mysql_error("Could not connect to the database.  Please contact a site administrator for assistance!")); 
echo "Connected to the database";
?>

 

Any assistance would be greatly appreciated.

Okay, I changed it to this:

 

<?php
mysql_connect("localhost", "username", "password") or die("Could not connect to the database.  Please contact a site administrator for assistance!"); 
echo "Connected to the database";
?>

 

And now I am getting this:

 

Could not connect to the database. Please contact a site administrator for assistance!PHP Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ttracker'@'localhost' (using password: YES) in C:\DATA\ttracker.dsolutionsgroup.com\htdocs\Connections\time_tracker_connection.php on line 2

 

Is there a way to suppress the PHP Warning part of the connection error?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.