【问题标题】:Unable to connect to MySQL using PHP on Windows7无法在 Windows7 上使用 PHP 连接到 MySQL
【发布时间】:2017-05-15 11:57:00
【问题描述】:

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

致命错误:调用未定义函数 mysql_connect

我尝试了以下 stackoverflow 解决方案,但在 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)?提前致谢!

【问题讨论】:

  • 您是否尝试过使用mysqli_ 进行连接/查询?您没有发布任何 php/mysql 代码,所以很难说。
  • 我会尽快尝试 mysqli_ 版本的代码。我尝试使用 mysql_connect("$host", "$username", "$password") 后跟 mysql_select_db("$db_name") 的简单连接脚本,这在我用 PHP 部署 wamp 服务器的旧 XP 机器上运行良好5.2.5
  • 这也可能是权限问题。您是否尝试以管理员身份运行?如果你还没有。 Win7 可以这样敏感。您说您进行了手动安装;您应该尝试卸载并选择自动安装,如果使用 32,请选择正确的安装,如果您有 64 位系统,请使用 64 位。
  • Deprecated 表示不再支持或推荐它,并且会发出警告(但仍应运行)。你是对的,它应该在 PHP 5.6.29 中仍然可以工作。

标签: php mysql windows-7


【解决方案1】:

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

解决方案 1:检查 MySQL 支持

Detecting mysql support in php

使用上面的链接来确定 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 注入,如果您不花时间升级代码,您的网站可能容易受到黑客攻击。

【讨论】:

  • "到 7.0 之前的版本" - OP 的 PHP 版本是 5.6.29
  • 好的,那么您可能仍然会收到有关 mysql_connect 已被弃用的警告。尝试 5.5 之前的版本(如果您使用解决方案 1 并且安全性不是很大的威胁)。
  • 在 phpinfo() 页面中它没有显示任何关于 MySQL 甚至 MySQLi 的信息。它显示的只是“mysqlnd”。意思是mysqli也不行,也就是说这个PHP版本不支持? @LStarky
  • 正确。因此,您需要重新安装 WAMP 并确保包含 PDO 和 MySQL 支持。
  • 如何确定? @LStarky
猜你喜欢
  • 2010-10-14
  • 2018-12-11
  • 2018-08-23
  • 2016-11-29
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 2015-01-22
相关资源
最近更新 更多