PHP/Apache:PHP 致命错误:调用未定义的函数 mysql_connect()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15346605/
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-25 09:05:05  来源:igfitidea点击:

PHP/Apache: PHP Fatal error: Call to undefined function mysql_connect()

phpmysqlapache

提问by Cheddar

I have MySQL (5.5.23 Community Server), Apache (2.2), and PHP (5.3.13) running on a Fedora 15 machine (64-bit). Each works with no problem on their own.

我在 Fedora 15 机器(64 位)上运行 MySQL(5.5.23 社区服务器)、Apache(2.2)和 PHP(5.3.13)。每个工作都没有问题。

I thought that PHP was configured/compiled to work with MySQL, but when I try to load any PHP web page which makes a MySQL call then I get the above noted fatal error (in the Apache error log). PHP works okay on its own though, through Apache and at the command line.

我认为 PHP 已配置/编译为与 MySQL 一起使用,但是当我尝试加载任何进行 MySQL 调用的 PHP 网页时,我收到上述致命错误(在 Apache 错误日志中)。但是,通过 Apache 和命令行,PHP 可以自己正常工作。

When I run php -i from the command line then sure enough it shows all the MySQL bits and pieces, but when I load a page displaying phpinfo() then there are no MySQL modules mentioned.

当我从命令行运行 php -i 时,它确实显示了所有 MySQL 的点点滴滴,但是当我加载一个显示 phpinfo() 的页面时,没有提到 MySQL 模块。

I've also tried un-commenting "extension=mysql.so" in php.ini, but PHP then complains with "PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysql.so'."(even though the module does reside there). But then I'm not entirely certain whether that line is required to be un-commented or not. [correction: the error stated was actually due to a typo, but running from the command line shows that the module is already loaded anyway - there is a warning from PHP that says so.]

我也试过在 php.ini 中取消注释“extension=mysql.so”,但是 PHP 然后抱怨“PHP 启动:无法加载动态库'/usr/lib64/php/modules/mysql.so'."(即使模块确实驻留在那里)。但后来我不是完全确定是否需要取消注释该行。[更正:声明的错误实际上是由于打字错误,但从命令行运行显示该模块无论如何都已加载 - PHP 发出警告这么说。]

I've also disabled SELinux; stopped/restarted Apache numerous times; yummed MySQL-Devel, in case it made any difference; and, it seems, every other possible thing.

我也禁用了 SELinux;多次停止/重新启动Apache;yummed MySQL-Devel,以防万一它有什么不同;而且,似乎,所有其他可能的事情。

If I run a little PHP test script from the command line, to access the MySQL database, then it works with no problem, so PHP does know about MySQL, but when it runs under Apache it seems to have no clue that MySQL even exists.

如果我从命令行运行一个小的 PHP 测试脚本来访问 MySQL 数据库,那么它就没有问题,所以 PHP 确实知道 MySQL,但是当它在 Apache 下运行时,它似乎不知道 MySQL 甚至存在。

I've been trying on and off for months to solve this problem, and seem to have tried everything, but nothing seems to work.

几个月来我一直在断断续续地尝试解决这个问题,似乎已经尝试了一切,但似乎没有任何效果。

I don't mind having to re-install PHP if I really have to, if somebody can demonstrate the proper way to configure/compile with MySQL support, so that I can get it to work under Apache, although ideally I'd simply prefer to get the existing installation working.

如果我真的必须重新安装 PHP,我不介意,如果有人可以演示使用 MySQL 支持配置/编译的正确方法,以便我可以让它在 Apache 下工作,尽管理想情况下我只是更喜欢使现有的安装工作。

回答by andreimarinescu

In case anyone else faces this, it's a case of PHP not having access to the mysql client libraries. Having a MySQL server on the system is not the correct fix. Fix for ubuntu (and PHP 5):

如果其他人遇到这种情况,这是 PHP 无法访问 mysql 客户端库的情况。在系统上安装 MySQL 服务器并不是正确的解决方法。修复 ubuntu(和 PHP 5):

sudo apt-get install php5-mysql

After installing the client, the webserver should be restarted. In case you're using apache, the following should work:

安装客户端后,应重新启动网络服务器。如果您使用的是 apache,以下应该可以工作:

sudo service apache2 restart

回答by Tim Penner

Keep in mind that as of PHP 5.5.0 the mysql_connect()function is deprecated, and it is completely removed in PHP 7

请记住,从 PHP 5.5.0 开始,该mysql_connect()函数已被弃用,并在 PHP 7 中完全删除

More info can be found on the php documentation:

更多信息可以在php 文档中找到:

Quote:

引用:

Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
* mysqli_connect()
* PDO::__construct()

警告
此扩展在 PHP 5.5.0 中已弃用,并在 PHP 7.0.0 中删除。相反,应使用 MySQLi 或 PDO_MySQL 扩展。另请参阅 MySQL:选择 API 指南和相关常见问题以了解更多信息。此函数的替代方法包括:
* mysqli_connect()
* PDO::__construct()

回答by che-azeh

I had this same problem and had to refer to the php manualwhich told me the mysql and mysqli extensions require libmysql.dll to load. I searched for it under C:\windows\system32 (windows 7) and could not find, so I downloaded it hereand placed it in my C:\windows\system32. I restarted Apache and everything worked fine. Took me 3 days to figure out, hope it helps.

我遇到了同样的问题,不得不参考php 手册,它告诉我 mysql 和 mysqli 扩展需要加载 libmysql.dll。我在C:\windows\system32(windows 7)下搜索,没找到,所以在这里下载,放在我的C:\windows\system32下。我重新启动了 Apache,一切正常。我花了 3 天时间才弄明白,希望对您有所帮助。

回答by James C

The Apache module PHP version might for some odd reason not be picking up the php.ini file as the CLI version I'd suggest having a good look at:

Apache 模块 PHP 版本可能出于某种奇怪的原因没有选择 php.ini 文件作为 CLI 版本我建议好好看看:

  • Any differences in the .inifiles that differ between php -iand phpinfo()via a web page*
  • If there are no differences then to look at the permissions of mysql.soand the .inifiles but I think that Apache parses these as the rootuser
  • 网页.ini之间php -iphpinfo()通过网页不同的文件中的任何差异*
  • 如果没有区别,则查看mysql.so.ini文件的权限,但我认为 Apache 将这些解析为root用户

To be really clear here, don't go searching for php.inifiles on the file system, have a look at what PHP says that it's looking at

在这里要说清楚,不要php.ini在文件系统上搜索文件,看看 PHP 说它在看什么

回答by Rahim Khoja

How about

怎么样

sudo yum install php-mysql

or

或者

sudo apt-get install php5-mysql

回答by Gordon

I had received a similar error message. Think I inadvertently typed "9o" at start of first line of php.ini file. Once I removed that, I no longer received the "fatal error" messages. Hope this helps.

我收到了类似的错误消息。认为我无意中在 php.ini 文件的第一行开头输入了“9o”。一旦我删除了它,我就不再收到“致命错误”消息。希望这可以帮助。

回答by pixeltracer

The mysqldeamon should be running.

mysql守护进程应该正在运行。

If not try this:

如果没有试试这个:

#/etc/init.d/mysql start

Or this:

或这个:

#service mysqld start

And if you want to add mysql on boot:

如果你想在启动时添加 mysql:

# chkconfig --add mysqld
# chkconfig -- level 235 mysqld on

If yes, and it is still not working try this:

如果是,并且仍然无法正常工作,请尝试以下操作:

Uncomment the following lines in /etc/php/php.ini

取消注释 /etc/php/php.ini 中的以下几行

extension=mysqli.so
extension=mysql.so

And please check your post above '/usr/lib64/php/modules/msql.so'. It should be mysql.so(if it's mistyped ignore it...)

请检查您在'/usr/lib64/php/modules/msql.so'上方帖子。它应该是mysql.so(如果输入错误,请忽略它...)