Home > Database > Mysql Tutorial > Why Does PHP 7 Throw a \'Fatal error: Call to undefined function mysql_connect()\' Error?

Why Does PHP 7 Throw a \'Fatal error: Call to undefined function mysql_connect()\' Error?

Barbara Streisand
Release: 2024-11-23 08:20:40
Original
216 people have browsed it

Why Does PHP 7 Throw a

PHP Error Handling: "Fatal error: Call to undefined function mysql_connect() [duplicate]"

Question:

When attempting to connect to a MySQL database using PHP, you encounter the error message "Fatal error: Call to undefined function mysql_connect()." Despite configuring PHP, MySQL, and Apache correctly, this issue persists.

Answer:

This error typically arises when you have recently upgraded your PHP version to PHP 7. In this version, the mysql_connect() function has been deprecated. To resolve this issue:

  1. Verify your PHP version:

    Use the command php -version to check your PHP version.

  2. Switch to mysqli_connect():

    Replace the deprecated mysql_connect() function with its corresponding mysqli_connect() function. For example:

    $host = "127.0.0.1";
    $username = "root";
    $pass = "foobar";
    $con = mysqli_connect($host, $username, $pass, "your_database");
    Copy after login
  3. Upgrade legacy PHP code:

    If you are working with legacy PHP code, you must upgrade all instances of mysql_ functions to mysqli_ functions.

The above is the detailed content of Why Does PHP 7 Throw a \'Fatal error: 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template