Home >Database >Mysql Tutorial >Why is `mysql_connect()` Deprecated in PHP 7 and How Do I Fix It?

Why is `mysql_connect()` Deprecated in PHP 7 and How Do I Fix It?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-22 03:27:11720browse

Why is `mysql_connect()` Deprecated in PHP 7 and How Do I Fix It?

Undefined Function 'mysql_connect()' in PHP

You've configured PHP, MySQL, and Apache, and localhost() functions correctly in PHP. However, after installing MySQL, you encounter the error:

Fatal error: Call to undefined function mysql_connect()

This issue arises because you have upgraded to PHP 7, where the mysql_connect function is deprecated. To resolve it:

  1. Check your PHP version with php -version.
  2. Replace mysql_connect with mysqli_connect in your code:
$host = "127.0.0.1";
$username = "root";
$pass = "foobar";
$con = mysqli_connect($host, $username, $pass, "your_database");

If you're migrating legacy PHP code, you'll need to convert all your mysql_* functions to mysqli_* equivalents.

The above is the detailed content of Why is `mysql_connect()` Deprecated in PHP 7 and How Do I Fix It?. 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