Jump to content

[SOLVED] Displaying custom error on connection failure to database


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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.