简体   繁体   中英

PHP connect to external MySQL Database

im trying to connect to my external sql database but I keep getting this error:

mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\zconnect.php on line 10
Unable to connect to database! Please try again later.

This is the PHP script:

<?php
$hostname='111.11.111.11'; #this is not my real ip address
$username='root';
$password='password';
$dbname='database';
$usertable='users';
$yourfield = 'admin';

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);
}
?>

Tried this but didn't work also:

Alias /phpmyadmin "c:/wamp/apps/phpmyadmin4.1.14/"

# to give access to phpmyadmin from outside 
# replace the lines
#
# Require local
#
# by
#
# Require all granted
#

Any help is appreciated!

First, I suggest SSH-ing your server and TCPing-ing or Netcat-ing (ok, I'm stopping) your remote MySQL server, so you see if it really can be accessed. Some hosts - mainly shared - require you to ask them to enable remote MySQL first (some have this option on CPanel).

Second, some MySQL servers whitelist some clients to access itself, exclusively. phpMyAdmin can help you in reading and setting this configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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