开发者

unable to connect mysql db with this code

开发者 https://www.devze.com 2023-03-22 02:32 出处:网络
$hostname=\'example.com\'; $username=\'someUser\'; $password=\'password\'; $dbname=\'aDataBase\'; $usertable=\'my_table\';
$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?


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.


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());

0

精彩评论

暂无评论...
验证码 换一张
取 消