Home  >  Article  >  Backend Development  >  How to Resolve \"Fatal error: Call to undefined function mysql_connect()\" in PHP?

How to Resolve \"Fatal error: Call to undefined function mysql_connect()\" in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-10-18 17:42:03922browse

How to Resolve

Understanding the "Fatal error: Call to undefined function mysql_connect()" Issue

When encountering this error message in your PHP script, it indicates that the mysql_connect() function is missing or not recognized by your system. This error often occurs after upgrading to PHP version 7.0 or higher.

Cause of the Error

In PHP 7.0, the mysql_* functions, including mysql_connect(), were deprecated due to concerns about security and performance. As a result, these functions are no longer available and should be replaced with their mysqli or PDO counterparts.

Solution: Upgrade to mysqli_connect

To resolve this error, you will need to upgrade your code to use the mysqli extension. Here's how:

  1. Use the Correct Function: Replace mysql_connect() with mysqli_connect().
  2. Provide Connection Parameters: Ensure you provide the correct connection parameters, including the host, username, password, and database name.

Example:

<code class="php">$host = "127.0.0.1";
$username = "root";
$pass = "foobar";
$con = mysqli_connect($host, $username, $pass, "your_database");</code>
  1. Upgrade Legacy Code: If you're upgrading legacy PHP code, you will need to replace all occurrences of mysql_* functions with equivalent mysqli or PDO functions.

Remember that upgrading to PHP 7.0 or higher requires a careful review of your existing code to ensure compatibility. By replacing deprecated functions with their modern counterparts, you can eliminate potential errors and maintain the security and performance of your PHP applications.

The above is the detailed content of How to Resolve \"Fatal error: Call to undefined function mysql_connect()\" in PHP?. 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