Home  >  Article  >  Backend Development  >  Why am I getting the \"Fatal error: Uncaught Error: Call to undefined function mysql_connect()\" error in PHP?

Why am I getting the \"Fatal error: Uncaught Error: Call to undefined function mysql_connect()\" error in PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-11-04 01:38:30136browse

Why am I getting the

Error: "Fatal error: Uncaught Error: Call to undefined function mysql_connect() "

A user encounters a "Fatal error: Uncaught Error: Call to undefined function mysql_connect()" error while attempting to connect to a MySQL server.

Explanation:

The error indicates that the "mysql_connect()" function used in the PHP code is no longer supported. This function was deprecated in PHP 5.5 and removed entirely in PHP 7.

Alternatives:

To address this error, you need to replace "mysql_connect()" with either MySQLi or PDO. Both MySQLi and PDO are modern, object-oriented interfaces for interacting with MySQL.

Using MySQLi:

Example code:

<code class="php">$mysqli = new mysqli($mysql_hostname, $mysql_username, $mysql_password, $mysql_database);</code>

Using PDO:

Example code:

<code class="php">try {
    $db = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_database", $mysql_username, $mysql_password);
} catch (PDOException $e) {
    echo "Error occurred: " . $e->getMessage();
}</code>

By replacing "mysql_connect()" with MySQLi or PDO, you can establish a connection to the MySQL server and perform database operations without encountering the undefined function error.

The above is the detailed content of Why am I getting the \"Fatal error: Uncaught Error: Call to undefined function mysql_connect()\" error 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