简体   繁体   English

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

[英]Fatal error: Call to undefined function mysql_connect()

I have set up PHP, MySQL, and Apache.我已经设置了 PHP、MySQL 和 Apache。 localhost() for PHP and it is working well. localhost()用于 PHP 并且运行良好。 But after I downloaded MySQL, it reports:但是在我下载 MySQL 后,它报告:

Fatal error: Call to undefined function mysql_connect()致命错误:调用未定义的函数 mysql_connect()

How can I fix this?我怎样才能解决这个问题?

You upgraded to PHP 7, and now mysql_connect is deprecated.您升级到 PHP 7,现在不推荐使用mysql_connect Check yours with:检查你的:

php -version

Change it to mysqli_connect as in:将其更改为mysqli_connect如下所示:

$host = "127.0.0.1";
$username = "root";
$pass = "foobar";
$con = mysqli_connect($host, $username, $pass, "your_database");

If you're upgrading legacy PHP, now you're faced with the task of upgrading all your mysql_* functions with mysqli_* functions.如果您正在升级旧的PHP,现在你面临着升级所有的任务mysql_*功能与mysqli_*功能。

If you get this error after upgrading to PHP 7.0, then you are using deprecated libraries.如果您在升级到 PHP 7.0 后收到此错误,那么您使用的是已弃用的库。

mysql_connect — Open a connection to a MySQL Server mysql_connect — 打开一个到 MySQL 服务器的连接
Warning警告
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.这个扩展在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除。 Instead, the MySQLi or PDO_MySQL extension should be used.相反,应使用 MySQLi 或 PDO_MySQL 扩展。

More here: http://php.net/manual/en/function.mysql-connect.php更多信息: http : //php.net/manual/en/function.mysql-connect.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 5:如果您正在运行 PHP,您还需要为 mysql 5 安装 php 模块:

sudo apt-get install php5-mysql

Verify that your installation of PHP has been compiled with mysql support.验证您的 PHP 安装是否已编译支持 mysql。 Create a test web page containing <?php phpinfo(); exit(); ?>创建一个包含<?php phpinfo(); exit(); ?>的测试网页<?php phpinfo(); exit(); ?> <?php phpinfo(); exit(); ?> <?php phpinfo(); exit(); ?> and load it in your browser. <?php phpinfo(); exit(); ?>并将其加载到浏览器中。 Search the page for MySQL.在页面中搜索 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如果你没有看到它,你需要重新编译支持 MySQL 的 PHP,或者重新安装一个内置了它的 PHP 包

PHP.INI

Check if you forgot to enable the options below(loads the modules for mysql among others):检查您是否忘记启用以下选项(加载 mysql 等模块):

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "ext"

Use.采用。 <?php $con = mysqli_connect('localhost', 'username', 'password', 'database'); ?>

In PHP 7. You probably have PHP 7 in XAMPP.在 PHP 7 中。您可能在 XAMPP 中有 PHP 7。 You now have two option: MySQLi and PDO.您现在有两个选择:MySQLi 和 PDO。

This error is coming only for your PHP version v7.0.此错误仅适用于您的 PHP 版本 v7.0。 you can avoid these using PHP v5.0 else您可以使用 PHP v5.0 else 避免这些

use it用它

mysqli_connect("localhost","root","")

i made only mysqli from mysql我只从 mysql 制作了 mysqli

For CPanel users, if you see this error and you already have PHP 5.x selected for the site, there might be a CPanel update that disabled mysql and mysqli PHP extensions.对于CPanel用户,如果您看到此错误并且您已经为站点选择了 PHP 5.x,则可能有一个CPanel更新禁用了mysqlmysqli PHP 扩展。

To check and enable the extensions:要检查和启用扩展:

  1. Go to Select PHP Version in CPanel转到在CPanel 中选择 PHP 版本
  2. Make sure you have PHP 5.x selected确保您选择了 PHP 5.x
  3. Make sure mysql and mysqli PHP extensions are checked确保mysqlmysqli PHP 扩展被选中

I had this same problem on RHEL6.我在 RHEL6 上遇到了同样的问题。 It turns out that the mysql.ini file in /etc/php.d only had a module name but needed a full path name.原来 /etc/php.d 中的 mysql.ini 文件只有一个模块名,但需要一个完整的路径名。 On my RHEL6 system the entry that works is:在我的 RHEL6 系统上,有效的条目是:

; Enable mysql extension module

extension=/usr/lib64/php/modules/mysql.so

After modifying the file, I restarted apache and everything worked.修改文件后,我重新启动了apache,一切正常。

A solution could be to use adapter functions like these to start using mysqli instead of mysql over your entire application:一个解决方案可能是使用这些适配器函数来开始在整个应用程序中使用mysqli而不是mysql

if (!function_exists('mysql_connect')) {
    function mysql_connect($host = '', $user = '', $password = '', $database = '', $port = 0, $socket = '') {
        return mysqli_connect($host, $user, $password, $database, $port, $socket);
    }
}


if (!function_exists('mysql_select_db')) {
    function mysql_select_db($link, $dbname) {
        mysqli_select_db($link, $dbname);
    }
}

Am using windows 8 n this issue got resolved by changing the environment variables我正在使用 Windows 8 n 这个问题通过更改环境变量得到解决

follow these steps: Open my computer properties->advanced system settings->Environment variables.按照以下步骤操作:打开我的电脑属性->高级系统设置->环境变量。 Under 'system variables', select 'path' and click on 'edit' In 'variable value', add 'C:\\php;'在“系统变量”下,选择“路径”并点击“编辑”在“变量值”中,添加“C:\\php;” OR the path where php installed.或者安装php的路径。

click OK and apply the settings and restart the system.单击“确定”并应用设置并重新启动系统。 It should work.它应该工作。

Here is a quick fix:这是一个快速修复:

All the pros will probably hate me for this answer.所有的专业人士可能会因为这个答案而讨厌我。 But I got the same error on a server: Fatal error: Uncaught Error: Call to undefined function mysql_connect() that was using PHP 7. Did not have time to rewrite all the mysql code so a quick, temporary fix if anyone needs it is in CPANEL to look for PHP Configuration and change the version for that account to something like PHP 5.4 instead of PHP 7. Then the code worked fine without the above error.但是我在服务器上遇到了同样的错误:致命错误:未捕获的错误:调用使用 PHP 7 的未定义函数 mysql_connect() 。没有时间重写所有 mysql 代码,所以如果有人需要,可以快速临时修复它在 CPANEL 中查找PHP 配置并将该帐户的版本更改为 PHP 5.4 而不是 PHP 7。然后代码运行良好,没有上述错误。

If you are using Windows10, PHP 7.2 and try to connect to mysql.如果您使用的是 Windows10、PHP 7.2 并尝试连接到 mysql。 If this error occurred如果发生此错误

Uncaught Error: Call to undefined function mysqli() in未捕获的错误:调用未定义的函数 mysqli()

The do the following steps to get it correct.执行以下步骤以使其正确。

  1. Go to the PHP installation folder,转到PHP安装文件夹,
  2. CHeck for php.ini file, (Only dev, prod file is there, then one of the file as php.ini file)检查 php.ini 文件,(只有 dev、prod 文件存在,然后文件之一为 php.ini 文件)
  3. Look for "extension=mysqli" and remove the ";"查找“extension=mysqli”并删除“;” before it.在它之前。
  4. Look for "extension_dir" and mentioned the path of "ext" directory.查找“extension_dir”并提到“ext”目录的路径。
  5. Restart the application.重新启动应用程序。

Hope this helps to someone.希望这对某人有帮助。

我的解决方案在这里(我只需要从PHPIniDir 'c:\\PHP\\'删除最后一个斜杠(注意:反斜杠): 致命错误:调用未定义的函数 mysql_connect() 无法解决

Check if mysqli module is installed for your PHP version检查是否为您的 PHP 版本安装了 mysqli 模块

$ ls /etc/php/7.0/mods-available/mysql*
/etc/php/7.0/mods-available/mysqli.ini /etc/php/7.0/mods-available/mysqlnd.ini

Enable the module启用模块

$ sudo phpenmod mysqli

This Code Can Help You此代码可以帮助您

<?php 

$servername = "localhost";
$username = "root";
$password = "";


$con = new mysqli($servername, $username, $password);

?>

But Change The "servername","username" and "password"但更改“服务器名”、“用户名”和“密码”

The problem shows up when you're using mysql_connect in your code lines so just add it as in mysqli_connect and it will solve the problem.当您在代码行中使用mysql_connect时会出现问题,因此只需将其添加为mysqli_connect解决问题。
You also have to then use mysqli throughout your code lines or the problem would surface again.然后,您还必须在整个代码行中使用mysqli ,否则问题会再次浮出水面。
The example mysql_select_db will then be mysqli_select_db for selecting Database.示例mysql_select_db将是用于选择数据库的mysqli_select_db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM