简体   繁体   中英

Why can I connect to my hosted website database using localhost?

This might be a stupid question but why am i using local host if my site is being hosted with rackspace?

$db_host = "localhost";
$db_username = "*****";
$db_pass = "********";
$db_name = "lds";


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

It appears to be getting a connection because I'm successfully echoing 'Connected successfully'.

Maybe something is terribly wrong because i noticed when i took out localhost from both mysql_connect function and from the db_host variable... it still says connected successfully.

Would that be because i've already wired the ftp connections up in my IDE?

Any help would be greatly appreciated. Thanks in advance.

Your database server is running locally to the remote machine, where you are executing your PHP script(s). To your script the database appears to be localhost even though the web server and database server are remote relative to you.

Per the documentation for mysql_connect :

If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'.

This would seem to indicate that if no value for the database server is provided, the default is used. Also note that the documentation also states that this function is deprecated in PHP 5.5.0.

See instead: http://php.net/mysqli and http://php.net/pdo

只要您的php我们的服务器和MySQL数据库服务器在同一台机器上,本地主机就可以工作

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