2

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I tried to connect mysql database.but am getting the following error.

Warning: mysql_connect(): [2002] No connection could be made because the target machine actively (trying to connect via tcp://localhost:3306) in test.php on line 5

Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in test.php on line 5 Warning: mysql_close() expects parameter 1 to be resource, boolean given in test.php on line 15

test.php:

<?php
$link = mysql_connect(localhost, dbuser, dbpass);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Community
  • 1
  • 1
jsk
  • 29
  • 1
  • 1
  • 4
  • hopefully there is no such thing as MTSQI – ajreal Dec 05 '11 at 17:05
  • 3
    Is `No connection could be made because the target machine actively refused it` not very clear? – Pekka Dec 05 '11 at 17:06
  • That is the warning am getting when i run the test.php file – jsk Dec 05 '11 at 17:19
  • The code you have written is trying to open a connection to MySQL on the `localhost` ie the same machine that the PHP is running on ... do you have MySQL installed and running ? or are you trying to connect to a different MySQL server ? – Manse Dec 05 '11 at 17:21
  • What is your OS? Did you change/set any firewall settings? – Emre Erkan Dec 06 '11 at 13:42

4 Answers4

8

This happened to us when running a PHP & MySQL on IIS7 in Windows Server 2008 in VirtualBox. It turns out that the Windows hosts file didn't have an IPv4 entry for localhost (127.0.0.1) so the route to MySQL on localhost was not found.

We added the following to the hosts file which resolved it:

127.0.0.1       localhost # Additional IPv4 entry
::1             localhost # Existing IPv6 localhost entry

Reference: A brief description of PHP/MySQL Localhost via IPv4 & IPv6

Eric Kigathi
  • 1,815
  • 21
  • 23
1

In my.ini find string:

bind-address = 127.0.0.1

Change from localhost to network IP.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Nikolay
  • 11
  • 1
1

Got the same message trying to connect to 'localhost' by mistake (on a Windows server). After fixing the connection string to the correct (remote) server the error disappeared.

1

The MySQL server isn't running, or you have a firewall blocking port 3306.

This error message means the system did not accept the TCP connection request.

Justin ᚅᚔᚈᚄᚒᚔ
  • 15,081
  • 7
  • 52
  • 64