Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

php - 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:

<?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).

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.

$hostname='113.101.88.97.ukld.db.5510597.hostedresource.com';
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the supplied domain name 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...