Jump to content

SQL Error - no good!


iMiles

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/203299-sql-error-no-good/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/203299-sql-error-no-good/#findComment-1065143
Share on other sites

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.