Reputation: 15298
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';
Upvotes: 15
Views: 81331
Reputation: 1134
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.
Upvotes: 1
Reputation: 6152
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.
BTW, in most cases you cannot run PHP script from html file, rename your file extension to php
Good luck!
Upvotes: 2
Reputation: 1944
You need to use either the hostname or the IP address, not both.
If you have a command line MySQL client available to you, you can test your connection strings.
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!
Upvotes: 10
Reputation: 8886
No need for IP address Try this
<?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);
}
?>
Upvotes: 5
Reputation: 53636
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.
Upvotes: 1
Reputation: 48887
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.
Upvotes: 12
Reputation: 181450
No. Do not mix IP Addresses with host names. You should connect by using just the hostname:
$hostname='ukld.db.5510597.hostedresource.com';
Upvotes: 2