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!

11
  • 1
    do you can connect in this server by any tool on your machine? Commented Jan 3, 2016 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. Commented Jan 3, 2016 at 16:16
  • For all it's worth, do not use mysql_* functions. Commented Jan 3, 2016 at 16:16
  • 1
    Stop using mysql. It's deprecated and as of php 7 completely gone. I recomment using PDO. Else, you can use MYSQLi Commented Jan 3, 2016 at 16:25
  • 1
    Please stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy. Commented Jan 3, 2016 at 16:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can't seem to connect. I've tried changing this: gyazo.com/5d33a22beeb91fadf7ed21a12f6b2dd2 But didnt work :(
I see you've fixed this by setting permission. It's what I said on "Second" :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.