php 远程连接到 MySQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4683554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remotely connecting to a MySQL database
提问by djq
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. 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).
我对php和MySQL很陌生,我不明白一些事情。我已将上述代码保存在一个文件(称为 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. However, I don't know if that is structured correctly.
我假设我需要包含 IP 地址(我在使用 PhPMyAdmin 登录时看到的),所以我也添加了它。但是,我不知道这是否结构正确。
$hostname='113.101.88.97.ukld.db.5510597.hostedresource.com';
采纳答案by webbiedave
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. No need to do it yourself. Plus, hardcoding IPs is bad practice as they can change over time.
主机名将在幕后为您转换为 IP 地址。没必要自己做。另外,硬编码 IP 是不好的做法,因为它们会随着时间的推移而改变。
回答by minichate
You need to use eitherthe 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 应用程序!
回答by Wazy
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);
}
?>
回答by fatnjazzy
Host name represents an IP address.
for example: stackoverflow.com represents the IP 64.34.119.12.
if you ping to stackoverflow.com the IP 64.34.119.12 will answer you.
It means that the host can be, IP address or host-name.
So you should try ukld.db.5510597.hostedresource.com, or 173.201.88.97. but not together.
主机名代表一个 IP 地址。
例如:stackoverflow.com 代表 IP 64.34.119.12。
如果您 ping 到 stackoverflow.com,IP 64.34.119.12 会回答您。
这意味着主机可以是,IP 地址或主机名。
所以你应该尝试 ukld.db.5510597.hostedresource.com 或 173.201.88.97。但不能在一起。
BTW, in most cases you cannot run PHP script from html file, rename your file extension to php
顺便说一句,在大多数情况下,您无法从 html 文件运行 PHP 脚本,请将文件扩展名重命名为 php
Good luck!
祝你好运!
回答by nathant23
You don't need to include the IP address
您不需要包含 IP 地址
回答by Pablo Santa Cruz
No. Do not mix IP Addresses with host names. You should connect by using just the hostname:
不可以。不要将 IP 地址与主机名混合使用。您应该仅使用主机名进行连接:
$hostname='ukld.db.5510597.hostedresource.com';
回答by Alex Howansky
You don't need the IP address, just use the domain name you were given. Your server will use DNS to resolve it to the proper IP.
您不需要 IP 地址,只需使用您提供的域名即可。您的服务器将使用 DNS 将其解析为正确的 IP。
回答by DK250
Don't save it as an HTML file. It is a PHP file. So save it as demo.php. It should have indicated where the error is if you had saved it as a PHP file.
不要将其保存为 HTML 文件。它是一个 PHP 文件。所以保存为demo.php。如果您将其保存为 PHP 文件,它应该会指出错误的位置。