致命错误:在第 2 行调用 C:\Apache\htdocs\test.php 中未定义的函数 mysql_connect()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5179535/
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
Fatal error: Call to undefined function mysql_connect() in C:\Apache\htdocs\test.php on line 2
提问by user389055
Fatal error: Call to undefined function mysql_connect() in C:\Apache\htdocs\test.php on line 2
致命错误:在第 2 行调用 C:\Apache\htdocs\test.php 中未定义的函数 mysql_connect()
I've spent 11 hours today trying to get past this ONE problem.
我今天花了 11 个小时试图解决这个 ONE 问题。
I have installed: MySQL 5.1 Apache 2.2.14 PHP 5.2.17
我已安装: MySQL 5.1 Apache 2.2.14 PHP 5.2.17
these were the versions referred to in the book "PHP and MySQL".
这些是“PHP 和 MySQL”一书中提到的版本。
When I run the script:
当我运行脚本时:
<?php
mysql_connect ('localhost', 'root', 'password');
?>
where localhost, root and password are the REAL values for this given test system and all I have seen is:
其中 localhost、root 和 password 是这个给定测试系统的真实值,我所看到的是:
Fatal error: Call to undefined function mysql_connect() in C:\Apache\htdocs\test.php on line 2
致命错误:在第 2 行调用 C:\Apache\htdocs\test.php 中未定义的函数 mysql_connect()
回答by binaryLV
Uncomment the line extension=php_mysql.dll
in your "php.ini" file and restart Apache.
取消注释extension=php_mysql.dll
“php.ini”文件中的行并重新启动 Apache。
Additionally, "libmysql.dll" file must be available to Apache, i.e., it must be either in available in Windows systems PATH or in Apache working directory.
此外,“libmysql.dll”文件必须对Apache 可用,即它必须在Windows 系统PATH 或Apache 工作目录中可用。
See more about installing MySQL extension in manual.
在手册中查看有关安装 MySQL 扩展的更多信息。
P.S. I would advise to consider MySQL extension as deprecated and to use MySQLi or even PDO for working with databases (I prefer PDO).
PS 我建议将 MySQL 扩展视为已弃用,并使用 MySQLi 甚至 PDO 来处理数据库(我更喜欢 PDO)。
回答by Darius Miliauskas
I had the similar issue. I solved it the following way after a number of attempts to follow the pieces of advice in the forums. I am reposting the solution because it could be helpful for others.
我有类似的问题。在多次尝试遵循论坛中的建议后,我通过以下方式解决了它。我正在重新发布解决方案,因为它可能对其他人有所帮助。
I am running Windows 7 (Apache 2.2 & PHP 5.2.17 & MySQL 5.0.51a), the syntax in the file "httpd.conf" (C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\httpd.conf) was sensitive to slashes. You can check if "php.ini" is read from the right directory. Just type in your browser "localhost/index.php". The code of index.php is the following:
我正在运行 Windows 7(Apache 2.2 & PHP 5.2.17 & MySQL 5.0.51a),文件“httpd.conf”中的语法(C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\ httpd.conf) 对斜杠很敏感。您可以检查是否从正确的目录中读取了“php.ini”。只需在浏览器中输入“localhost/index.php”。index.php 的代码如下:
<?php
echo phpinfo();
?>
<?php
echo phpinfo();
?>
There is the row (not far from the top) called "Loaded Configuration File". So, if there is nothing added, then the problem could be that your "php.ini" is not read, even you uncommented (extension=php_mysql.dll and extension=php_mysqli.dll). So, in order to make it work I did the following step. I needed to change from
有一行(离顶部不远)称为“加载的配置文件”。因此,如果没有添加任何内容,那么问题可能是您的“php.ini”未被读取,即使您取消注释(extension=php_mysql.dll 和 extension=php_mysqli.dll)。所以,为了使它工作,我做了以下步骤。我需要改变
PHPIniDir 'c:\PHP\'
PHPIniDir 'c:\PHP\'
to
到
PHPIniDir 'c:\PHP'
PHPIniDir 'c:\PHP'
Pay the attention that the last slash disturbed everything!
注意最后一个斜线扰乱了一切!
Now the row "Loaded Configuration File" gets "C:\PHP\php.ini" after refreshing "localhost/index.php" (before I restarted Apache2.2) as well as mysql block is there. MySQL and PHP are working together!
现在,在刷新“localhost/index.php”(在我重新启动 Apache2.2 之前)以及 mysql 块之后,“加载的配置文件”行获得了“C:\PHP\php.ini”。MySQL 和 PHP 正在协同工作!
回答by Ashok
Change the database.phpfile from
将database.php文件从
$db['default']['dbdriver'] = 'mysql';
to
到
$db['default']['dbdriver'] = 'mysqli';