无法通过PHP连接到MySQL

前端之家收集整理的这篇文章主要介绍了无法通过PHP连接到MySQL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
看起来像一个初学者的问题,但我似乎无法弄清楚…

我有一个全新的Windows 7 x64 / Apache 2.2 / PHP 5.2 / MysqL 5 x64安装.
实际上我尝试了IIS和Apache.

但是这个PHP代码不起作用:

<?
$hostname = "localhost";
$username = "xxx";
$password = "xxx";

$db = MysqL_connect($hostname,$username,$password) or die("die");
?>

>我可以使用直接命令行将OK连接到MysqL数据库,当然使用相同的登录名/密码.
>我可以使用相同的登录名/密码通过网络访问MysqL数据库.
> PHP正在工作(echo(“hello world”)确实有效).
>在PHP.ini中启用了MysqL扩展.事实上,如果我禁用它,我会调用未定义的函数MysqL_connect().
> Windows防火墙已禁用.
>没有错误消息.它只是没有返回任何东西. 600秒后,它超时:

Fatal error: Maximum execution time of 60 seconds exceeded in
        C:\Ampache\try.PHP on line 6

任何线索?

等待等待……我想我发现了……

RTM他们说:-)这一切都在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.

实际上它可以使用

$host = "127.0.0.1";

谢谢你的关注 !

猜你在找的PHP相关文章