Home > Backend Development > PHP Tutorial > Why Am I Still Getting \'Undefined Function mysql_connect()\' Error After Installing PHP5-mysql?

Why Am I Still Getting \'Undefined Function mysql_connect()\' Error After Installing PHP5-mysql?

DDD
Release: 2024-11-04 01:59:29
Original
580 people have browsed it

Why Am I Still Getting

Error Resolution: Understanding "Undefined Function mysql_connect() [duplicate]"

The "Undefined function mysql_connect()" error indicates that a PHP script is attempting to use the mysql_* functions, which are deprecated and removed in PHP7. Upon installing PHP5-mysql and restarting MySQL/Apache 2, you still encounter this error. This article delves into the solution to this issue.

PHP7 Removal of mysql_* Functions

In PHP7, the mysql_ functions have been completely removed. This includes the mysql_connect() function used to establish a connection to a MySQL database. Instead, you should utilize PDO-functions or mysqli_ functions.

Workaround for PHP7

If updating your code to PDO-functions or mysqli_* functions is not an option, consider the following workaround:

Create a PHP include file named "fix_mysql.inc.php" and include it in your PHP script that uses the mysql_ functions. This file recreates the old mysql_ functions using the mysqli_*()-functions.

// fix_mysql.inc.php

<?php

function mysql_connect($host, $username, $password) {
  return mysqli_connect($host, $username, $password);
}

function mysql_close($link) {
  return mysqli_close($link);
}

// ... Define other mysql_* functions as needed ...

?>
Copy after login

Example Usage

Include the "fix_mysql.inc.php" file in your PHP script:

<?php
include 'fix_mysql.inc.php';

$link = mysql_connect('localhost', 'root', 'mypassword');
// Continue using the mysql_* functions as before
?>
Copy after login

Note: This workaround only recreates the basic functionality of the mysql_ functions. For advanced scenarios, it is recommended to update your code to PDO-functions or mysqli_ functions.

The above is the detailed content of Why Am I Still Getting \'Undefined Function mysql_connect()\' Error After Installing PHP5-mysql?. 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