user849137
user849137

Reputation:

unable to connect mysql db with this code

$hostname='example.com';
$username='someUser';
$password='password';
$dbname='aDataBase';
$usertable='my_table';

mysql_connect($hostname,$username,$password);
@mysql_select_db($dbname) or die( "Unable to select database");

I'm using a read-only username here. When i test the script, i get the error "unable to select database".

Any clue?

Upvotes: 0

Views: 248

Answers (2)

Andy
Andy

Reputation: 21

Try this

$db_name = "DATABASE";
$db_user = "USER";
$db_pass = "1234";
$db_serv = "localhost";

$res = @mysql_connect($db_serv,$db_user ,$db_pass);

if($res)
    mysql_select_db($db_name) or die(mysql_error());

Upvotes: 0

Marc B
Marc B

Reputation: 360692

Don't use fixed strings for your die() message...They're useless for diagnosis. As well, don't supress errors with @.

mysql_connect($hostname,$username,$password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

Using this you'll get the exact reason things are failing.

Upvotes: 2

Related Questions