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

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

DDD
DDDOriginal
2024-11-04 00:27:30603browse

Why am I getting the

Unresolved Connection Error: "Fatal Error: Uncaught Error: Call to undefined function mysql_connect()"?

When attempting to establish a database connection using XAMPP and MySQL, you may encounter the error "Fatal error: Uncaught Error: Call to undefined function mysql_connect()". This issue arises due to the deprecation of mysql_* functions in PHP 7.

Reason for the Error:

The deprecated mysql_ functions, such as mysql_connect(), are no longer supported in PHP 7. If you are using XAMPP with PHP 7, you will experience this error when trying to use the mysql_ functions.

Alternatives to mysql_* Functions:

To resolve this issue, you have two options:

  • MySQLi: MySQLi is a mysqli_* function extension that provides an improved interface for interacting with MySQL.
  • PDO (PHP Data Objects): PDO is a database abstraction layer that allows you to interact with different types of databases using a consistent API.

For example, if you were using mysql_connect() before, you can use mysqli_connect() in its place:

<?php
// 旧方式 (不再支持)
$link = mysql_connect($mysql_hostname , $mysql_username);

// 新方式 (使用 MySQLi)
$link = mysqli_connect($mysql_hostname , $mysql_username);
?>

Similarly, you can use the mysqli_ or PDO functions to replace other mysql_ functions. Detailed documentation and examples are available for both MySQLi and PDO.

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