views:

399

answers:

2
+9  A: 

Wait Wait Wait ... I think I found it ...

RTFM they said :-) It's all at http://php.net/manual/fr/function.mysql-connect.php :

Bruce Kirkpatrick - 28-Oct-2009 05:48 :
On Windows Vista or above, an entry in the Windows/System32/drivers/etc/hosts file causes mysql_connect() connections to "localhost" to timeout and never connect. This happens on php 5.3 and above since it now uses mysql native driver which has changed it connection behavior compared to libmysql.dll in previous versions. It is not a PHP bug, but definitely a configuration issue for users on new windows systems.

To get around this, you must remove the entry like this:
::1 localhost

and make sure you still have:
127.0.0.1 localhost

Also, you could change the code to connect to the ip instead, but that is inconvenient if you have many web sites.

This issue occurs on Windows Vista, Windows 7 and Windows Server 2008.

In fact it works with

$host = "127.0.0.1";

Thanks for your interest !

Jalil
I too had this problem on Win7. Spent hours figuring this our for myself when I was testing the 5.3 beta (no documentation back then). - A regression "bug" in M$s newest systems that breaks most code written for the most popular alternative to ISS/.Net ... shocking, right? xD
Atli
In fact I'm not shocked to see the setting : ::1 localhostwhich appears to me as correct ... But I'm shocked that : - at end of 2009, PHP/MySQL does not handle yet IPV6 - PHP/MySQL just freezes instead of handling better the connexion problem, with a serious error message ... What do you think ?
Jalil
Just went through the same thing 6 months later. Wish it had turned up in google a couple of hours earlier in the trouble shooting!
Rob Walker
A: 

In case you whant localhost to work, you could edit your hosts file in C:\Windows\System32\drivers\etc., or use another dns

kek