php+mysql 致命错误:调用未定义的函数 mysql_connect() in
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15615244/
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
php+mysql Fatal error: Call to undefined function mysql_connect() in
提问by Smajl
I have searched the internet for troubleshooting with this "Call to undefined function mysql_connect() " error but none of the suggested procedures works...
我已经在互联网上搜索了这个“调用未定义函数 mysql_connect()”错误的故障排除,但没有一个建议的程序有效......
When I try to access mysql from php, I get this error. I have PHP Version 5.2.17 and MySQL 5.1.68 (mysql is running outside php, I tried creating tables and databases etc.).
当我尝试从 php 访问 mysql 时,出现此错误。我有 PHP 版本 5.2.17 和 MySQL 5.1.68(mysql 在 php 之外运行,我尝试创建表和数据库等)。
I uncomment the extensions in php.ini:
我取消注释 php.ini 中的扩展名:
extension=php_mysql.dll
extension=php_mysqli.dll
and specified the path to these extensions.
并指定这些扩展的路径。
I also added both PHP and MySQL to my PATH variable. I am using Apache 2.2. and I restarted the server after every change that I made.
我还将 PHP 和 MySQL 添加到我的 PATH 变量中。我正在使用 Apache 2.2。每次更改后我都重新启动了服务器。
This is my code in php for accessing the database (but I suppose that the problem is not in syntax):
这是我在 php 中访问数据库的代码(但我认为问题不在于语法):
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pasw';
$dbname = 'db';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
I am out of ideas how to fix this, can u suggest some other troubleshooting tips? I am quite new to his problematics... I would like to use MySQL, cause thats what I will use on the actual server when I′ll have my website up and running. Thanks in advance!
我不知道如何解决这个问题,你能建议一些其他的故障排除技巧吗?我对他的问题很陌生...我想使用 MySQL,因为当我启动并运行我的网站时,我将在实际服务器上使用它。提前致谢!
EDIT: this is from apache log:
编辑:这是来自 apache 日志:
[Mon Mar 25 13:50:42 2013] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined function mysql_connect() in C:\Web\thenemis\dbconnect.php on line 7, referer: http://localhost/thenemis/admin.php
[Mon Mar 25 13:50:42 2013] [error] [client 127.0.0.1] File does not exist: C:/Web/favicon.ico
EDIT2: this is from php info (I think that the problem may be with some unspecified parametres in php.ini file). I havent read anywhere that I should specify some other features than extensions and path to them but maybe I was wrong... what do you suggest?
EDIT2:这是来自 php info (我认为问题可能出在 php.ini 文件中的一些未指定的参数上)。我没有读过任何地方我应该指定一些其他功能而不是扩展和路径,但也许我错了......你有什么建议?
mysql
MySQL Support enabled
Active Persistent Links 0
Active Links 0
Client API version 5.0.51a
Directive Local Value Master Value
mysql.allow_persistent On On
mysql.connect_timeout 60 60
mysql.default_host no value no value
mysql.default_password no value no value
mysql.default_port no value no value
mysql.default_socket no value no value
mysql.default_user no value no value
mysql.max_links Unlimited Unlimited
mysql.max_persistent Unlimited Unlimited
mysql.trace_mode Off Off
回答by Chris
As it was already said use phpinfo() for determining if mysql is running correctly. In general you should not use mysql anymore more but rather the new improved version of mysql, called mysqli http://php.net/manual/de/book.mysqli.php
正如已经说过的,使用 phpinfo() 来确定 mysql 是否正确运行。一般来说,你不应该更多地使用 mysql,而应该使用 mysql 的新改进版本,称为 mysqli http://php.net/manual/de/book.mysqli.php
You can in addition programmatically check if a function exists with method_exists method
此外,您还可以通过 method_exists 方法以编程方式检查函数是否存在
回答by Matheus Lopes
Try to use this:
尝试使用这个:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pasw';
$dbname = 'db';
<?php
// Create connection
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
MySQL support will be removed of PHP, so start to use MySQLi or PDO instead.
PHP 将删除 MySQL 支持,因此开始使用 MySQLi 或 PDO。
:)
:)
回答by Aman Garg
Verify that your installation of PHP has been compiled with mysql support. Create a test php file containing <?php phpinfo(); exit(); ?>
and run it in your browser. Search the page for keyword 'MySQL'. If you don't see it, you need to recompile PHP with MySQL support, or reinstall a PHP package that has it built-in.
验证您安装的 PHP 是否已编译支持 mysql。创建一个包含测试 php 文件<?php phpinfo(); exit(); ?>
并在浏览器中运行它。在页面中搜索关键字“MySQL”。如果你没有看到它,你需要重新编译支持 MySQL 的 PHP,或者重新安装一个内置了它的 PHP 包。
Open your terminal and run bellow command.
打开终端并运行以下命令。
sudo apt-get install mysql-server
If you are running PHP you will also need to install the php module for mysql
如果您正在运行 PHP,您还需要为 mysql 安装 php 模块
sudo apt-get install php5-mysql
回答by user3765786
I set the path for PHP in the system variable of my computer, edited the path value to C:/program files/php;
and enabled extension=php_mysqli.dll
in php.ini
.
我在我的计算机的系统变量中设置的路径PHP,编辑的路径值C:/program files/php;
并启用extension=php_mysqli.dll
的php.ini
。
Then I used this function
然后我用了这个功能
mysqli_connect($host,$username,$password)or die(mysql_error());
And after that a system restart. It worked for me; I hope it works for you.
之后系统重新启动。它对我有用;我希望这个对你有用。