Home >Database >Mysql Tutorial >Why is my PHP code throwing a 'Undefined Function mysql_connect()' error even after installing php5-mysql?

Why is my PHP code throwing a 'Undefined Function mysql_connect()' error even after installing php5-mysql?

DDD
DDDOriginal
2024-12-01 05:43:10741browse

Why is my PHP code throwing a

Undefined Function mysql_connect()

This error is encountered when trying to use the MySQL connectivity functions in PHP, specifically mysql_connect(). This problem usually arises when the PHP MySQL extension is not properly installed or configured.

As per the provided details, you have installed the php5-mysql package, but the error persists. Here are a few steps to troubleshoot the issue:

Update Code Syntax

In PHP 7, the mysql_* functions are no longer supported. Instead, you should use the PDO (PHP Data Objects) functions or the mysqli_* functions. Update your code accordingly.

Enable MySQL Extension

Ensure that the PHP MySQL extension is enabled in your PHP configuration. Check the phpinfo() output to verify this. If the extension is not listed, you can use the following Ubuntu commands to enable it:

sudo apt-get install php-mysql
sudo phpenmod mysql

Restart Apache

After enabling the extension, restart the Apache web server to apply the changes:

sudo service apache2 restart

Alternative Fix

If updating your code or enabling the MySQL extension is not feasible, you can use a workaround by creating a PHP include file that recreates the old mysql_* functions using mysqli_* functions. This file, known as fix_mysql.inc.php, is available online and can be included in your PHP code.

The above is the detailed content of Why is my PHP code throwing a 'Undefined Function mysql_connect()' error even after installing php5-mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn