Jump to content
xisto Community
Sign in to follow this  
kvkv

Suppressing Mysql Error In Php How to suppress auto generated mysql err

Recommended Posts

I am testing my website on my local machine. It is still in development stage and I am using php/mysql. If there is a mysql connection error, I am checking for the connection variable and displaying a proper error message. But even before it displays my error message, it displays an error message on browser thrown by mysql (MySQL Connection Failed: Access denied for user).Can I suppress this message and display only my message?

Share this post


Link to post
Share on other sites

From the php.net site, we have this function:

<?php// Turn off all error reportingerror_reporting(0);  ?>
found
here. http://forums.xisto.com/no_longer_exists/
And I think that there is a way to use the '@' symbol in front of the function, but I couldn't source a link. Maybe someone else has a link for that.

But suppressing the errors won't make the logic or processes work any differently. It is best to find out what is causing the error in the first place. Since you are 'developing' the program, I would leave the full error reporting "ON" until the bugs are under control. Clearly, you want to know at this stage if anything is going wrong and fix it before implementing the code into the production environment. And using the '@' Error suppression on a function is not reccomended at the development stage, either, for the same reasons. If you are unable to connect, you want to know why that is the case and find the solution using the error reporting output.

Share this post


Link to post
Share on other sites

i think this may help you:

<?$hostname = "localhost";$database = "yourdatase";$username = "user";$password = "pass";@ $conn = mysql_pconnect($hostname, $username, $password);if(!$conn){	echo "ERROR: Cannot connect to database. Please try again later!";	exit;}//select the database$db = mysql_select_db($database);if(!$db){	echo "ERROR: Cannot select database. Please try again later!";	exit;}?>

Share this post


Link to post
Share on other sites

i think this may help you:

<?$hostname = "localhost";$database = "yourdatase";$username = "user";$password = "pass";@ $conn = mysql_pconnect($hostname, $username, $password);if(!$conn){	echo "ERROR: Cannot connect to database. Please try again later!";	exit;}//select the database$db = mysql_select_db($database);if(!$db){	echo "ERROR: Cannot select database. Please try again later!";	exit;}?>

226032[/snapback]

That is what I was doing exactly. But still it was printing standard error as well as my error. I had to set error_reporting level as jlhaslip suggested. Thanks!

Share this post


Link to post
Share on other sites

Provided the function being called does not explicitly send any output (ie. not as an error), prefixing it with an '@' symbol causes any errors to be supressed for that single instance only.

For example:

echo file_get_contents('non_existant_file');

Will, provided error_reporting is turned on, output an error about how PHP could not find the file specified.

However:

echo @file_get_contents('non_existant_file');

Will prevent any error from being displayed (even though the error itself does actually occur).

Instead of:

@ $conn = mysql_pconnect($hostname, $username, $password);

Try:

$conn = @mysql_pconnect($hostname, $username, $password);

Hope that makes sense.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.