简体   繁体   English

远程连接MySQL数据库

[英]Remotely connecting to a MySQL database

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: 我刚刚在网络托管服务上建立了一个MySQL数据库,我正在尝试使用以下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);
}
?>

I'm quite new to php and MySQL, and I don't understand a few things. 我是php和MySQL的新手,我不懂一些东西。 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). 我已将上述代码保存在一个文件(名为demo.html)中,我尝试在我的Web浏览器中查看它(它目前没有显示任何内容)。

My hosting company told me that to connect to the database I should use 我的托管公司告诉我要连接到我应该使用的数据库

ukld.db.5513497.hostedresource.com

I assumed that I needed to include the IP address (what I see when I login using PhPMyAdmin), so I added that also. 我假设我需要包含IP地址(我在使用PhPMyAdmin登录时看到的内容),所以我也添加了。 However, I don't know if that is structured correctly. 但是,我不知道这是否结构正确。

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

Use the supplied domain name ukld.db.5510597.hostedresource.com 使用提供的域名ukld.db.5510597.hostedresource.com

Prepending the hostname with an IP as you are doing only changes the hostname and that is why it is failing to connect. 使用IP预先添加主机名只会更改主机名,这就是无法连接的原因。

The hostname will be converted to an IP address behind the scenes for you. 主机名将在幕后转换为IP地址。 No need to do it yourself. 不需要自己动手。 Plus, hardcoding IPs is bad practice as they can change over time. 此外,硬编码IP是不好的做法,因为它们可能会随着时间的推移而发生变化。

You need to use either the hostname or the IP address, not both. 你需要为使用主机名或IP地址,而不是两个。

If you have a command line MySQL client available to you, you can test your connection strings. 如果您有可用的命令行MySQL客户端,则可以测试连接字符串。

mysql -u myusername -p -h ukld.db.5510597.hostedresource.com -D testdb

It should then prompt you for your password. 然后它应该提示您输入密码。 If it connects successfully transfer those settings to your PHP app! 如果连接成功将这些设置传输到您的PHP应用程序!

No need for IP address Try this 不需要IP地址试试这个

<?php
//Connect To Database
$hostname='ukld.db.5510597.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);
}
?>

您不需要包含IP地址

No. Do not mix IP Addresses with host names. 不。不要将IP地址与主机名混合使用。 You should connect by using just the hostname: 您应该只使用主机名进行连接:

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

Host name represents an IP address. 主机名代表IP地址。
for example: stackoverflow.com represents the IP 64.34.119.12. 例如:stackoverflow.com代表IP 64.34.119.12。
if you ping to stackoverflow.com the IP 64.34.119.12 will answer you. 如果你ping到stackoverflow.com,IP 64.34.119.12将回答你。
It means that the host can be, IP address or host-name. 这意味着主机可以是IP地址或主机名。
So you should try ukld.db.5510597.hostedresource.com, or 173.201.88.97. 所以你应该试试ukld.db.5510597.hostedresource.com,或173.201.88.97。 but not together. 但不是在一起。

BTW, in most cases you cannot run PHP script from html file, rename your file extension to php 顺便说一句,在大多数情况下你不能从html文件运行PHP脚本,将你的文件扩展名重命名为php

Good luck! 祝好运!

Don't save it as an HTML file. 不要将其另存为HTML文件。 It is a PHP file. 这是一个PHP文件。 So save it as demo.php. 所以保存为demo.php。 It should have indicated where the error is if you had saved it as a PHP file. 如果您将其保存为PHP文件,它应该指出错误的位置。

You don't need the IP address, just use the domain name you were given. 您不需要IP地址,只需使用您提供的域名。 Your server will use DNS to resolve it to the proper IP. 您的服务器将使用DNS将其解析为正确的IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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