Home > Database > Mysql Tutorial > Why is my PHP code throwing a \'Call to undefined function mysql_connect()\' error?

Why is my PHP code throwing a \'Call to undefined function mysql_connect()\' error?

DDD
Release: 2024-11-20 18:21:13
Original
300 people have browsed it

Why is my PHP code throwing a

Error Resolution: Understanding "Call to Undefined Function mysql_connect() Error" in PHP

Problem:

When attempting to connect to a MySQL database using PHP, users may encounter the error "Fatal error: Call to undefined function mysql_connect()." This issue arises after setting up PHP, MySQL, and Apache.

Explanation:

This error indicates that the deprecated mysql_connect() function is being called in PHP 7 or a later version. PHP 7 and above no longer support the mysql_connect() function as it has been replaced by the mysqli_connect() function.

Solution:

To resolve this issue, upgrade your MySQL-related functions to use the new mysqli_* syntax. Specifically, replace mysql_connect() with mysqli_connect().

Here's an example of the updated PHP code:

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

Additional Considerations:

  • Ensure that the MySQL extension is configured and enabled in your PHP installation.
  • Confirm that the MySQL user has the necessary privileges to connect to the specified database.

The above is the detailed content of Why is my PHP code throwing a 'Call to undefined function mysql_connect()' error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template