robert_gsfame Posted April 11, 2011 Share Posted April 11, 2011 mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); i create a connection and let say there is a problem with the server so that the connection can't established my question is how can i avoid the default error to be displayed which is WARNING: mysql_connect() blablabla as it is annoying to see such error message. thx in advance Quote Link to comment https://forums.phpfreaks.com/topic/233342-mysql_connect-problem/ Share on other sites More sharing options...
Vel Posted April 11, 2011 Share Posted April 11, 2011 You can use the following: <?php error_reporting(0); $con=mysql_connect("$host", "$username", "$password") if(!$con) die('Could not connect: ' . mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233342-mysql_connect-problem/#findComment-1199951 Share on other sites More sharing options...
Adam Posted April 11, 2011 Share Posted April 11, 2011 In a development environment you want those errors, so that you can spot them instantly and fix them. On a production environment however, you should have all error messages disabled anyway. If possible this should be set within the server's php.ini configuration file, but if not you can set it from within the code: ini_set('display_errors', 0); Quote Link to comment https://forums.phpfreaks.com/topic/233342-mysql_connect-problem/#findComment-1199953 Share on other sites More sharing options...
Adam Posted April 11, 2011 Share Posted April 11, 2011 You can use the following: <?php $con=mysql_connect("$host", "$username", "$password") if(!$con) die('Could not connect: ' . mysql_error()); ?> The PHP fatal error will still be shown.. Quote Link to comment https://forums.phpfreaks.com/topic/233342-mysql_connect-problem/#findComment-1199956 Share on other sites More sharing options...
Vel Posted April 11, 2011 Share Posted April 11, 2011 The PHP fatal error will still be shown.. Yea, I realised after he meant the big warning at the top. I just edited my code before seeing your response. Quote Link to comment https://forums.phpfreaks.com/topic/233342-mysql_connect-problem/#findComment-1199959 Share on other sites More sharing options...
Adam Posted April 11, 2011 Share Posted April 11, 2011 error_reporting() should still be enabled, albeit at a much higher level (fatal errors only), so that the server's error log still gets them. That way if users start reporting issues with the site, you can show them a "Sorry we're having technical difficulties" type error, but then look-up the actual error in the log. Best scenario would be also to get an email when ever a fatal error is triggered. Of course you don't want this on DEV, you just want all errors displayed instantly. Quote Link to comment https://forums.phpfreaks.com/topic/233342-mysql_connect-problem/#findComment-1199961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.