1

So i keep getting this error when trying to connect to my database remotely. The strange thing is it works for like 1/2 query(s) a minute but then after those query's it stops working and gives this error message. This just started happening randomly, before it used to work fine for many querys per second.

Details:

  • Im using xampp to host my database
  • The details to connect to the database are correct

Any ideas what might be happening?

Example of code (This is the code which I using to test the connection):

                $connection = mysql_connect('IP', 'DATABASE PASSWORD', 'PASSWORD', 'DATABSE NAME') or die(mysql_error());
                if (!$connection) 
                    die('Not connected : ' . mysql_error());

                $db_selected = mysql_select_db('DATABSE NAME', $connection);
                if (!$db_selected)
                    die ('Can\'t use : ' . mysql_error());

                $string = mysql_query("SELECT * FROM `characters` WHERE `name` LIKE 'gil'") or die(mysql_error());
                if($result = mysql_fetch_assoc($string))
                    echo $result['name'];

Full Error code:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'IP' (4) in /vhosts/DOMAIN/httpdocs/test.php on line 1
Can't connect to MySQL server on 'IP' (4)
j0k
  • 22,600
  • 28
  • 79
  • 90
Gil Julio
  • 812
  • 1
  • 9
  • 18
  • 2
    Sounds like you are calling `mysql_connect()` before every query, which is unnecessary. You should call it once at the top of your script. Although really you should call it never and use PDO or MySQLi. – DaveRandom May 28 '12 at 15:03
  • Any code example for us to see? – Nico May 28 '12 at 15:07
  • please attach the full error string and the piece of PHP code – dAm2K May 28 '12 at 15:09
  • Further to @DaveRandom's comment. [Best way to prevent SQL Injection in PHP](http://stackoverflow.com/a/60496/212940) – vascowhite May 28 '12 at 15:16
  • @DaveRandom Thats what im doing, I just made a test script to test this issue when it started occurring. – Gil Julio May 28 '12 at 15:30

1 Answers1

2

You are specifying wrong parameters to mysql_connect:

mysql_connect('IP', 'DATABASE PASSWORD', 'PASSWORD', 'DATABSE NAME')

Should be:

mysql_connect('server address', 'user name', 'password')

See the docs for more info.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 1
    Still happens, the thing is when i try from another computer to access the database remotely it works fine just doesnt work from one of my cloud servers. – Gil Julio May 28 '12 at 15:29
  • 1
    @Gil: You will need to allow the IP address of cloud servers from cpanel or control panel of website. Contact your server support team on how to whitelist the cloud server IPs for your db. – Sarfraz May 28 '12 at 15:30