-1

So I connect to my MySQL database using the following code:

function dbConnect($h,$u,$p,$n) {
  if (!$con = @mysql_connect($h,$u,$p)) {$err = err("There is a problem connecting to the database. ".mysql_error());}
  else if (!@mysql_select_db($n,$con)) {$err = err("The database \"{$n}\" could not be found. Check your spelling, make sure the database exists, and that your database credentials allows you access to this database.");}
  return (isset($err)) ? $err : "";
}

The problem is, if they put in a wrong username, mysql_connect will not see anything wrong with it and try to connect to the database, which outputs an error for the mysql_select_db().

So I found this link. Now normally removing the "any" user would be doable but I'm creating a phpMyAdmin-like tool and will be connecting to different databases with all types of configurations. How can I tell which username can actually connect? And how can I differentiate between the database name not existing and the username not having access to it?

Community
  • 1
  • 1
Err
  • 890
  • 2
  • 11
  • 32
  • What is this `if (!$con = @mysql_connect($h,$u,$p))`? – Lion Apr 17 '12 at 23:09
  • Try removing the @s. You might be quashing a useful error. – mqsoh Apr 17 '12 at 23:11
  • You're not following even the [link](http://stackoverflow.com/questions/7312401/php-mysql-connect-not-returning-false) you mentioned. – Lion Apr 17 '12 at 23:14
  • And you're not returning the connection on success either, you're returning the error. – Dale Apr 17 '12 at 23:21
  • What the... there is nothing wrong with the way I'm using mysql_connect(). I don't return the connection because I call the function which makes the connection to the database. I've tried and tested this and it works. – Err Apr 17 '12 at 23:21

1 Answers1

1

use mysql_errno()

see error codes here

Andreas Linden
  • 12,489
  • 7
  • 51
  • 67