远程连接到MySQL数据库

编程入门 行业动态 更新时间:2024-10-27 18:18:31
本文介绍了远程连接到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'; $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

我假设我需要包括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';

推荐答案

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

在执行操作时为主机名添加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数据库

本文发布于:2023-10-18 12:02:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   数据库   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!