iMiles Posted May 29, 2010 Share Posted May 29, 2010 So I came across a problem when I was making a form that allows a user to connect to their mySQL database. Here's the code: if ($_POST['submit']) { $sqlhost=$_POST['sqlhost']; $sqluser=$_POST['sqluser']; $sqlpass=$_POST['sqlpass']; $errorstring=""; //error checking if ($sqlhost=="") { $errorstring.="Host cannot be left empty!<br>"; } if ($sqluser=="") { $errorstring.="User cannot be left empty!<br>"; } //if their are errors display the error message if ($errorstring<>"") { echo "<center id=\"error\">$errorstring</center><p>"; } else //no errors mysql_connect("$sqlhost","$sqluser","$sqlpass") or die ("Unable to connect!"); Alright, that looks all good and dandy? It is. The only problem is if the user types in a database that we cannot connect to, it displays an error: Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'rssr' (1) in /Applications/XAMPP/xamppfiles/htdocs/install.php on line 40 Unable to connect! Thing is, I don't want it to display that error. I only want it to display my error, which is "Unable to connect", and not the computer generated error. Hope you understand what the hell I'm talking about :-\ - Miles Quote Link to comment https://forums.phpfreaks.com/topic/203299-sql-error-no-good/ Share on other sites More sharing options...
Mchl Posted May 29, 2010 Share Posted May 29, 2010 I'm pretty sure you should use 'localhost' as name of your MySQL server, not 'rssr' That's not what you're asking about. Should've read more carefully. Simplest way is to do it like this: if (!@mysql_connect()) { die('Unable to connect'); } although I'd prefer to use custom error handler as described here in Example #1 http://php.net/manual/en/function.set-error-handler.php and put database connection code in try-catch block Quote Link to comment https://forums.phpfreaks.com/topic/203299-sql-error-no-good/#findComment-1065143 Share on other sites More sharing options...
iMiles Posted May 29, 2010 Author Share Posted May 29, 2010 Thanks, I actually ended up using the error_reporting() function and setting it's value to 0. That fixed everything nicely! Quote Link to comment https://forums.phpfreaks.com/topic/203299-sql-error-no-good/#findComment-1065197 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.