0

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!

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Jeroen
  • 31
  • 2
  • 6
  • 1
    do you can connect in this server by any tool on your machine? – Gouda Elalfy Jan 03 '16 at 16:15
  • Is the remote database accessible remotely? For example the server may have restrictions for remote access and only allows connections from specified IP addresses. – segFault Jan 03 '16 at 16:16
  • For all it's worth, do not use `mysql_*` functions. – hjpotter92 Jan 03 '16 at 16:16
  • Not a php issue, check if Mysql is available through any other tool first – Shahzaib Ali Khan Jan 03 '16 at 16:19
  • Either your IP address is wrong, the firewall is blocking the request, you aren't connecting to the right address, or the database server isn't listening on the right port. – Quentin Jan 03 '16 at 16:24
  • 1
    Stop using mysql. It's deprecated and as of php 7 completely gone. I recomment using PDO. Else, you can use MYSQLi – Chris G Jan 03 '16 at 16:25
  • "failed because the connected party did not properly respond after a period of time" . check Gouda Elafy's Comment – Shan Jan 03 '16 at 16:45
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 03 '16 at 16:49
  • @GoudaElalfy Its hosting a webserver + mysql, I connect to it with RDP – Jeroen Jan 03 '16 at 17:38
  • @JayBlanchard I have now switched to mysqli as the guy who commented it below – Jeroen Jan 03 '16 at 18:07
  • Tried connecting with HeidiSQL but timed out :( – Jeroen Jan 03 '16 at 18:21

1 Answers1

1

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.