php 未定义的函数 mysql_connect()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13825108/
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
Undefined function mysql_connect()
提问by user1765369
I have ran aptitude install php5-mysql(and restarted MySQL/Apache 2), but I am still getting this error:
我已经运行aptitude install php5-mysql(并重新启动 MySQL/Apache 2),但我仍然收到此错误:
Fatal error: Call to undefined function mysql_connect() in /home/validate.php on line 21
致命错误:在第 21 行调用 /home/validate.php 中未定义的函数 mysql_connect()
phpinfo()says the /etc/php5/apache2/conf.d/pdo_mysql.ini file has been parsed.
phpinfo()说 /etc/php5/apache2/conf.d/pdo_mysql.ini 文件已被解析。
采纳答案by wanovak
Well, this is your chance! It looks like PDOis ready; use that instead.
好吧,这是你的机会!看起来PDO已经准备好了;改用那个。
Try checking to see if the PHP MySQL extension module is being loaded:
尝试检查是否正在加载 PHP MySQL 扩展模块:
<?php
phpinfo();
?>
If it's not there, add the following to the php.inifile:
如果它不存在,请将以下内容添加到php.ini文件中:
extension=php_mysql.dll
回答by Sarcastron
I see that you tagged this with Ubuntu. Most likely the MySQL driver (and possibly MySQL) is not installed. Assuming you have SSHor terminal access and sudo permissions, log into the server and run this:
我看到你用 Ubuntu 标记了这个。很可能没有安装 MySQL 驱动程序(也可能是 MySQL)。假设您有SSH或终端访问权限和 sudo 权限,请登录服务器并运行以下命令:
sudo apt-get install mysql-server mysql-client php5-mysql
If the MySQL packages or the php5-mysql package are already installed, this will update them.
如果 MySQL 包或 php5-mysql 包已经安装,这将更新它们。
UPDATE
更新
Since this answer still gets the occasional click I am going to update it to include PHP 7. PHP 7 requires a different package for MySQL so you will want to use a different argument for the apt-get command.
由于这个答案仍然偶尔会被点击,我将更新它以包含PHP 7。PHP 7 需要不同的 MySQL 包,因此您需要为 apt-get 命令使用不同的参数。
sudo apt-get install mysql-server mysql-common php7.0 php7.0-mysql
And importantly, mysql_connect()has been deprecated since PHP v5.5.0. Refer the official documentation here: PHP: mysql_connect()
重要的是,mysql_connect()自 PHP v5.5.0 以来已被弃用。请参阅此处的官方文档:PHP: mysql_connect()
回答by rubo77
In case, you are using PHP7 already, the formerly deprecated functions mysql_*were removed entirely, so you should update your code using the PDO-functions or mysqli_*functions instead.
如果您已经在使用 PHP7,以前不推荐使用的函数mysql_*已被完全删除,因此您应该使用 PDO 函数或mysqli_*函数来更新您的代码。
If that's not possible, as a workaround, I created a small PHP include file, that recreates the old mysql_*functions with mysqli_*()-functions: fix_mysql.inc.php
如果这是不可能的,作为一种解决方法,我创建了一个小的 PHP 包含文件,它mysql_*使用-functions重新创建旧函数mysqli_*():fix_mysql.inc.php
回答by kujiy
If someone came here with the problem of docker php official images, type below command inside the docker container.
如果有人带着docker php 官方镜像的问题来到这里,请在 docker 容器内输入以下命令。
$ docker-php-ext-install mysql mysqli pdo pdo_mysql
For more information, please refer to the link above How to install more PHP extensionssection(But it's a bit difficult for me...).
有关更多信息,请参阅上面How to install more PHP extensions部分的链接(但对我来说有点困难......)。
Or this doc may help you.
或者这个文档可能会帮助你。
回答by apurva sharma
I was also stuck with the same problem of undefined MySQL_connect().I tried to make changes in PHP.ini file but it was giving me the same error. Then I came to this solution where I changed my code from depreciated php functions to new functions.
我也遇到了未定义的 MySQL_connect() 的同样问题。我试图在 PHP.ini 文件中进行更改,但它给了我同样的错误。然后我来到了这个解决方案,我将代码从折旧的 php 函数更改为新函数。
$con=mysqli_connect($host,$user,$password);
mysqli_select_db($con,dbname);
//To select the database
session_start(); //To start the session
$query=mysqli_query($con,your query);
//made query after establishing connection with database.
I hope this will help you . This solution is correctly working for me .
我希望这能帮到您 。这个解决方案对我来说是正确的。
EDIT:
编辑:
If you upgrade form old php you need to apt-get install php7.0-mysql
如果您从旧的 php 升级,则需要 apt-get install php7.0-mysql
回答by Dominic Amal Joe F
In php.ini file
在 php.ini 文件中
change this
改变这个
;extension=php_mysql.dll
into
进入
extension=php_mysql.dll
回答by fortune
Try:
尝试:
<?php
phpinfo();
?>
Run the page and search for mysql. If not found, run the following in the shell and restart the Apache server:
运行页面并搜索mysql. 如果未找到,请在 shell 中运行以下命令并重新启动 Apache 服务器:
sudo apt-get install mysql-server mysql-client php5-mysql
Also make sure you have all the following lines uncommented somewhere in your apache2.conf (or in your conf.d/php.ini) file, from
还要确保在 apache2.conf(或 conf.d/php.ini)文件中的某处取消注释所有以下行,来自
;extension=php_mysql.so
to
到
extension=php_mysql.so
回答by Muc
My guess is your PHP installation wasn't compiled with MySQL support.
我的猜测是您的 PHP 安装没有使用 MySQL 支持编译。
Check your configure command (php -i | grep mysql). You should see something like '--with-mysql=shared,/usr'.
检查您的配置命令 ( php -i | grep mysql)。您应该会看到类似'--with-mysql=shared,/usr'.
You can check for complete instructions at http://php.net/manual/en/mysql.installation.php. Although, I would rather go with the solution proposed by @wanovak.
您可以在http://php.net/manual/en/mysql.installation.php查看完整说明。虽然,我宁愿采用@wanovak 提出的解决方案。
Still, I think you need MySQL support in order to use PDO.
不过,我认为您需要 MySQL 支持才能使用 PDO。
回答by Dota2
The question is tagged with ubuntu, but the solution of un-commenting the extension=mysqli.dllis specific to windows. I am confused here?!, anyways, first thing run <? php phpinfo ?>and search for mysql*under Configurationheading. If you don't see such a thing implies you have not installed or enabled php-mysql. So first install php-mysql
问题被标记为ubuntu,但取消注释的解决方案extension=mysqli.dll特定于 Windows。我在这里很困惑?!无论如何,首先要运行<? php phpinfo ?>并mysql*在Configuration标题下搜索。如果您没有看到这样的东西,则表示您尚未安装或启用php-mysql. 所以首先安装php-mysql
sudo apt get install php-mysql
sudo apt get install php-mysql
This command will install php-mysqldepending on the phpyou have already installed, so no worries about the version!!.
此命令将php-mysql根据php您已安装的版本进行安装,因此无需担心版本!!
Then comes the unix specific solution, in the php.inifile un-comment the line
然后是unix特定的解决方案,在php.ini文件中取消注释该行
extension=msql.so
verify that msql.sois present in /usr/lib/php/<timestamp_folder>,
ELSE
验证msql.so存在于/usr/lib/php/<timestamp_folder>,
ELSE
extension=path/to/msql.so
Then finally restart the apacheand mysqlservices, and you should now see the mysqlsection under Configrationsheading in phpinfo page
然后最后重新启动apache和mysql服务,您现在应该看到标题mysql下的部分Configrationsphpinfo page
回答by freifede
I was getting this error because the project I was working on was developed on php 5.6 and after install, the project was unable to run on php7.1.
我收到此错误是因为我正在处理的项目是在 php 5.6 上开发的,安装后,该项目无法在 php7.1 上运行。
Just for anyone that uses Vagrant with ubuntu/nginx, in the nginx directory(/etc/nginx/), there is a directory named "sites-available" which contains a file named like the url configured for the vagrant maschine. In my case was homestead.app. Within this file there is a line that says something like
对于在 ubuntu/nginx 中使用 Vagrant 的任何人,在 nginx 目录(/etc/nginx/)中,有一个名为“sites-available”的目录,其中包含一个名为 vagrant 机器配置的 url 的文件。在我的例子中是 homestead.app。在此文件中,有一行内容类似于
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
There you can change the php version to the desired for that particular site.
在那里您可以将 php 版本更改为该特定站点所需的版本。
Googled this but wasnt really able to find a simple answer that said where to look and what to change.
用谷歌搜索这个,但并没有真正找到一个简单的答案,说明在哪里看和改变什么。
Hope that this helps anyone. Thanks.
希望这可以帮助任何人。谢谢。

