Home >Backend Development >PHP Tutorial >How to Fix \'Fatal Error: Call to Undefined Function mysql_connect()\'?

How to Fix \'Fatal Error: Call to Undefined Function mysql_connect()\'?

DDD
DDDOriginal
2024-10-18 17:38:29255browse

How to Fix

PHP Error: Unable to Connect to MySQL Database

You have encountered a "Fatal error: Call to undefined function mysql_connect()." This error indicates that PHP is unable to access the MySQL function mysql_connect().

Upon further investigation, you find that you have recently upgraded to PHP 7, which has deprecated the mysql_connect() function in favor of mysqli_connect().

Solution:

To resolve this error, you need to update your PHP code to use the mysqli_connect() function instead of mysql_connect(). Here's an example of how to do this:

<code class="php">$host = "127.0.0.1";
$username = "root";
$pass = "foobar";
$con = mysqli_connect($host, $username, $pass, "your_database");</code>

Additionally, if you are upgrading legacy PHP code, you will need to replace all instances of mysql_* functions with their corresponding mysqli_* counterparts.

The above is the detailed content of How to Fix \'Fatal Error: Call to Undefined Function mysql_connect()\'?. 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