远程连接到MySQL数据库 [英] Remotely connecting to a MySQL database

查看:82
本文介绍了远程连接到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在网络托管服务上设置了一个MySQL数据库,并尝试使用以下php远程连接到它:

I've just set up a MySQL database on a web-hosting service and I'm trying to connect to it remotely using the following php:

<?php
//Connect To Database
$hostname='113.101.88.97.ukld.db.5513497.hostedresource.com';
$username='myusername';
$password='mypassword';
$dbname='testdb';
$usertable='test';
$yourfield = 'lat';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);

$query = 'SELECT * FROM ' . $usertable;
$result = mysql_query($query);
if($result) {
    while($row = mysql_fetch_array($result)){
        print $name = $row[$yourfield];
        echo 'Name: ' . $name;
    }
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>

我对php和MySQL还是很陌生,我不了解几件事.我已将上面的代码保存在一个文件中(名为demo.html),并尝试在Web浏览器中查看它(当前它不显示任何内容).

I'm quite new to php and MySQL, and I don't understand a few things. I have saved the above code in a file (called demo.html) and I try viewing it in my web browser (it currently does not show anything).

我的托管公司告诉我,要连接到数据库,我应该使用

My hosting company told me that to connect to the database I should use

ukld.db.5513497.hostedresource.com

我假设我需要包括IP地址(使用PhPMyAdmin登录时看到的地址),因此我也添加了该地址.但是,我不知道它的结构是否正确.

I assumed that I needed to include the IP address (what I see when I login using PhPMyAdmin), so I added that also. However, I don't know if that is structured correctly.

$hostname='113.101.88.97.ukld.db.5510597.hostedresource.com';

推荐答案

使用提供的域名ukld.db.5510597.hostedresource.com

在执行操作时为主机名添加IP仅会更改主机名,这就是它无法连接的原因.

Prepending the hostname with an IP as you are doing only changes the hostname and that is why it is failing to connect.

主机名将为您转换为幕后IP地址.无需自己动手.另外,对IP进行硬编码是不明智的做法,因为IP会随着时间而变化.

The hostname will be converted to an IP address behind the scenes for you. No need to do it yourself. Plus, hardcoding IPs is bad practice as they can change over time.

这篇关于远程连接到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆