无法使用 Windows7 上的 PHP 连接到 MySQL

Unable to connect to MySQL using PHP on Windows7

我已经在 Windows7 上使用 Apache 2.4.23、PHP 5.6.29 和 MYSQL 服务器 5.5[=31= 手动设置了 wamp 服务器].每次我尝试连接到 MySQL 时,我都会收到错误消息:

fatal error: call to undefined function mysql_connect

我尝试了以下 Whosebug 解决方案,但我在 c:\PHP 中找不到任何 libmysql.dll 也没有复制 php_mysql.dll 从 c:\PHP 到 C:\Windows\System32 是富有成果的: Call to undefined function mysql_connect

我的 phpinfo() 的输出: https://jsfiddle.net/ad0gd90y/

我也查看了 Apache 中的错误日志文件:

Installing Apache HTTP Server 2.x with
 DomainName    = example.com
 ServerName    = www.example.com
 ServerAdmin   = admin@example.com
 ServerPort    = 80
 ServerSslPort = 443
 ServerRoot    = c:/Apache24
Rewrote docs/conf/extra/httpd-autoindex.conf.in
 to c:/Apache24/conf/original/extra/httpd-autoindex.conf
Rewrote docs/conf/extra/httpd-default.conf.in
 to c:/Apache24/conf/original/extra/httpd-default.conf
Rewrote docs/conf/extra/httpd-ssl.conf.in
 to c:/Apache24/conf/original/extra/httpd-ssl.conf
Rewrote docs/conf/extra/httpd-multilang-errordoc.conf.in
 to c:/Apache24/conf/original/extra/httpd-multilang-errordoc.conf
Rewrote docs/conf/extra/httpd-info.conf.in
 to c:/Apache24/conf/original/extra/httpd-info.conf
Rewrote docs/conf/extra/httpd-userdir.conf.in
 to c:/Apache24/conf/original/extra/httpd-userdir.conf
Rewrote docs/conf/extra/httpd-mpm.conf.in
 to c:/Apache24/conf/original/extra/httpd-mpm.conf
Rewrote docs/conf/httpd.conf.in
 to c:/Apache24/conf/original/httpd.conf
Rewrote docs/conf/extra/proxy-html.conf.in
 to c:/Apache24/conf/original/extra/proxy-html.conf
Rewrote docs/conf/extra/httpd-vhosts.conf.in
 to c:/Apache24/conf/original/extra/httpd-vhosts.conf
Rewrote docs/conf/extra/httpd-dav.conf.in
 to c:/Apache24/conf/original/extra/httpd-dav.conf
Rewrote docs/conf/extra/httpd-languages.conf.in
 to c:/Apache24/conf/original/extra/httpd-languages.conf
Rewrote docs/conf/extra/httpd-manual.conf.in
 to c:/Apache24/conf/original/extra/httpd-manual.conf
Duplicated c:/Apache24/conf/original/extra/httpd-autoindex.conf
 to c:/Apache24/conf/extra/httpd-autoindex.conf
Duplicated c:/Apache24/conf/original/extra/httpd-default.conf
 to c:/Apache24/conf/extra/httpd-default.conf
Duplicated c:/Apache24/conf/original/extra/httpd-ssl.conf
 to c:/Apache24/conf/extra/httpd-ssl.conf
Duplicated c:/Apache24/conf/original/extra/httpd-multilang-errordoc.conf
 to c:/Apache24/conf/extra/httpd-multilang-errordoc.conf
Duplicated c:/Apache24/conf/original/extra/httpd-info.conf
 to c:/Apache24/conf/extra/httpd-info.conf
Duplicated c:/Apache24/conf/original/extra/httpd-userdir.conf
 to c:/Apache24/conf/extra/httpd-userdir.conf
Duplicated c:/Apache24/conf/original/extra/httpd-mpm.conf
 to c:/Apache24/conf/extra/httpd-mpm.conf
Duplicated c:/Apache24/conf/original/httpd.conf
 to c:/Apache24/conf/httpd.conf
Duplicated c:/Apache24/conf/original/magic
 to c:/Apache24/conf/magic
Duplicated c:/Apache24/conf/original/charset.conv
 to c:/Apache24/conf/charset.conv
Duplicated c:/Apache24/conf/original/extra/proxy-html.conf
 to c:/Apache24/conf/extra/proxy-html.conf
Duplicated c:/Apache24/conf/original/extra/httpd-vhosts.conf
 to c:/Apache24/conf/extra/httpd-vhosts.conf
Duplicated c:/Apache24/conf/original/extra/httpd-dav.conf
 to c:/Apache24/conf/extra/httpd-dav.conf
Duplicated c:/Apache24/conf/original/mime.types
 to c:/Apache24/conf/mime.types
Duplicated c:/Apache24/conf/original/extra/httpd-languages.conf
 to c:/Apache24/conf/extra/httpd-languages.conf
Duplicated c:/Apache24/conf/original/extra/httpd-manual.conf
 to c:/Apache24/conf/extra/httpd-manual.conf

对于如何解决这个问题,我完全束手无策。另外 libmysql.dll 有什么用?有没有什么安装顺序可以搞定(我安装了apache,最后安装了php和mysql)?提前致谢!

很可能您正在将代码从旧站点复制到 PHP 的新版本。代码 mysql_connect 在 PHP 5.5 中弃用并在 PHP 7.0 中删除。您还需要确保 PHP 是在 MySQL 支持下编译的。

解决方案 1:检查 MySQL 支持

Detecting mysql support in php

使用上面的 link 来确定 PHP 是否使用 MySQL 支持编译。如果不是,则继续并在适当的支持(PDO 和 MySQL)下重新安装 PHP 或 WAMP。

解决方案 2:降级 PHP

如果问题与 mysql_connect 和相关 mysql_ 函数被弃用有关,您可以通过调整版本将 WAMP 安装中的 PHP 版本更改为早于 7.0 的版本它在工具栏的设置中使用。请注意,对于 5.5 和 7.0 之间的版本,您仍然可能会在错误日志中以及可能在代码输出中收到已弃用的函数警告,具体取决于您显示的错误(可以在 php.ini 中调整)。

解决方案 3:修复您的代码

尽管如此,最好的解决方案是升级您的 PHP 代码,使其不使用已弃用的 mysql_connectmysql_query 和其他相关遗留 mysql 职能。根据代码的大小,这可能是一个或长或短的过程。但是,考虑这样做很重要,因为较旧的 mysql 函数存在多个安全漏洞,例如 SQL 注入,如果您不花时间升级,您的网站可能容易受到黑客攻击你的代码。