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

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

Fatal error: Call to undefined function mysql_connect()

phpmysql

提问by pepsicode

I have set up PHP, MySQL, and Apache. localhost()for PHP and it is working well. But after I downloaded MySQL, it reports:

我已经设置了 PHP、MySQL 和 Apache。localhost()对于 PHP,它运行良好。但是在我下载 MySQL 后,它报告:

Fatal error: Call to undefined function mysql_connect()

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

How can I fix this?

我怎样才能解决这个问题?

回答by user3325593

You upgraded to PHP 7, and now mysql_connectis deprecated. Check yours with:

您升级到 PHP 7,现在mysql_connect已弃用。检查你的:

php -version

Change it to mysqli_connectas 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_*

回答by Nanhe Kumar

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

回答by argoden

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
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.

mysql_connect — 打开与 MySQL 服务器的连接
警告
该扩展在 PHP 5.5.0 中已被弃用,并在 PHP 7.0.0 中被删除。相反,应使用 MySQLi 或 PDO_MySQL 扩展。

More here: http://php.net/manual/en/function.mysql-connect.php

更多信息:http: //php.net/manual/en/function.mysql-connect.php

回答by Rohit Kumar Choudhary

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

回答by Afser2000

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"

回答by jamaley hussain

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

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

In PHP 7. You probably have PHP 7 in XAMPP. You now have two option: MySQLi and PDO.

在 PHP 7 中。您可能在 XAMPP 中有 PHP 7。您现在有两个选择:MySQLi 和 PDO。

回答by Kiran S youtube channel

This error is coming only for your PHP version v7.0. you can avoid these using PHP v5.0 else

此错误仅适用于您的 PHP 版本 v7.0。您可以使用 PHP v5.0 else 避免这些

use it

用它

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

i made only mysqli from mysql

我只从 mysql 制作了 mysqli

回答by Floris

A solution could be to use adapter functions like these to start using mysqliinstead of mysqlover 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);
    }
}

回答by ArtB

I had this same problem on RHEL6. It turns out that the mysql.ini file in /etc/php.d only had a module name but needed a full path name. On my RHEL6 system the entry that works is:

我在 RHEL6 上遇到了同样的问题。原来 /etc/php.d 中的 mysql.ini 文件只有一个模块名,但需要一个完整的路径名。在我的 RHEL6 系统上,有效的条目是:

; Enable mysql extension module

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

After modifying the file, I restarted apache and everything worked.

修改文件后,我重新启动了apache,一切正常。

回答by Bhavye

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;' OR the path where php installed.

按照以下步骤操作:打开我的电脑属性->高级系统设置->环境变量。在“系统变量”下,选择“路径”并点击“编辑”在“变量值”中,添加“C:\php;” 或者安装php的路径。

click OK and apply the settings and restart the system. It should work.

单击“确定”并应用设置并重新启动系统。它应该工作。