php 无法使用 mysql_* API 建立连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8388962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:35:39  来源:igfitidea点击:

No connection could be made using mysql_* API

phpmysqldatabase-connection

提问by jsk

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

可能重复:
mysql_fetch_array() 期望参数 1 是资源,选择中给出的布尔值

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

我尝试连接 mysql 数据库。但出现以下错误。

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

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

警告::mysql_connect()[2002] 无法建立连接,因为目标机器在第5行主动(试图通过 连接tcp://localhost:3306test.php

警告mysql_connect():无法建立连接,因为目标机器主动拒绝它。在第5行的 test.php 中警告:mysql_close()期望参数 1 是资源,test.php15行给出的布尔值

test.php:

测试.php:

<?php
$link = mysql_connect(localhost, dbuser, dbpass);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

回答by Eric Kigathi

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.

我们在 VirtualBox 中的 Windows Server 2008 中的 IIS7 上运行 PHP 和 MySQL 时发生了这种情况。事实证明,Windows 主机文件没有用于 localhost (127.0.0.1) 的 IPv4 条目,因此未找到到 localhost 上的 MySQL 的路由。

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

参考:PHP/MySQL Localhost 通过 IPv4 & IPv6 的简要说明

回答by Justin ??????

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

MySQL 服务器未运行,或者您的防火墙阻止了端口 3306。

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

此错误消息表示系统未接受 TCP 连接请求。

回答by Nikolay

In my.ini find string:

在 my.ini 中查找字符串:

bind-address = 127.0.0.1

Change from localhost to network IP.

从 localhost 更改为网络 IP。

回答by Ilshat Fattakhov

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.

尝试错误地连接到“本地主机”时收到相同的消息(在 Windows 服务器上)。将连接字符串固定到正确的(远程)服务器后,错误消失了。