Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No connection could be made using mysql_* API [duplicate]

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);
?>
like image 679
jsk Avatar asked Jan 18 '26 09:01

jsk


1 Answers

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

like image 194
Eric Kigathi Avatar answered Jan 20 '26 23:01

Eric Kigathi