Home > Database > Mysql Tutorial > Why Doesn't mysql_connect() Work in My PHP Code?

Why Doesn't mysql_connect() Work in My PHP Code?

DDD
Release: 2024-11-29 09:40:10
Original
698 people have browsed it

Why Doesn't mysql_connect() Work in My PHP Code?

Error: "Undefined Function mysql_connect()" in PHP

Introduction:
When attempting to establish a database connection using the mysql_connect() function, you may encounter the error "Call to undefined function mysql_connect()". This error indicates that the function is not recognized by PHP.

Issue:
The error typically arises when PHP is missing the necessary MySQL extension. Despite installing php5-mysql, the issue persists.

Resolution:

For PHP7 and Above:
In PHP7, the mysql_* functions have been removed due to deprecation. To resolve the error, you should update your code to use either PDO or MySQLi functions instead.

For PHP5:
Workaround:
If it's not feasible to update your code, you can utilize a workaround by creating an include file, fix_mysql.inc.php. This file recreates the old mysql_ functions with the mysqli_ functions. By including this file before using mysql_ functions, it will translate them to their mysqli_ equivalents. Here's an example of the workaround:

<?php

// Include the fix_mysql.inc.php file
include 'fix_mysql.inc.php';

// Use the mysql_* functions as usual
$conn = mysql_connect('hostname', 'username', 'password');

?>
Copy after login

Please note that this workaround is a temporary measure and it's still recommended to migrate your code to use PDO or MySQLi functions in the long run.

The above is the detailed content of Why Doesn't mysql_connect() Work in My PHP Code?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template