Home > Database > Mysql Tutorial > Why is `mysql_connect()` undefined in PHP 7 and how can I fix it?

Why is `mysql_connect()` undefined in PHP 7 and how can I fix it?

Barbara Streisand
Release: 2024-12-01 10:49:15
Original
769 people have browsed it

Why is `mysql_connect()` undefined in PHP 7 and how can I fix it?

PHP7 Fatal Error: mysql_connect() Undefined Function

When attempting to establish a connection between PHP and MySQL in XAMPP, an "Uncaught Error" may occur, indicating that the mysql_connect() function is undefined. This error often arises in PHP versions 7 onwards.

Issue Details

Specifically, the error manifests on line 22 of the code, where mysql_connect() is called to establish the connection.

$link = mysql_connect($mysql_hostname , $mysql_username);
Copy after login

Resolution

The issue stems from the deprecation of the MySQL extension in PHP 7. In its place, PHP7 provides two alternatives: MySQLi and PDO. Both offer similar functionalities to mysql_connect().

Alternative 1: MySQLi

Replace the mysql_connect() call with the following:

$link = mysqli_connect($mysql_hostname, $mysql_username, $mysql_password, $mysql_database);
Copy after login

Alternative 2: PDO

$link = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_database", $mysql_username, $mysql_password);
Copy after login

The above is the detailed content of Why is `mysql_connect()` undefined in PHP 7 and how can I fix it?. 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