简体   繁体   中英

Why does mysql_connect() return NULL?

I'm running my program on PHP 5.5.24,when I simply use :

$dblink = mysql_connect($dbhost, $dbuser, $dbpass);

But the function return NULL, I try to use mysql_error() to find out what's wrong, but it also returns an empty string. I know mysql_* is deprecated after 5.5.0, but I'm dealing with a very old repository. It will cost a lot of time to change into mysqli. The function suppose to refurn false if the connection failed, no NULL. Why does it happen?

-----Update-----

Now I find that $dblink can not shown by var_dump(), var_dump($dblink) would get NULL but it's actually not null.

似乎它可以返回null,搜索可能发生的情况的来源

I got the answer.

To show the result of mysql_connect(), pls use var_dump() instead of var_export(). I use var_export() and get NULL as return, but var_dump() shows the result is not null. That's strange.

mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = FALSE [, int $client_flags = 0 ]]]]] )

Returns a MySQL link identifier on success or FALSE on failure.

I think U have to try:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

To see what happened. I hope it is usefull.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM