Home  >  Article  >  Backend Development  >  解决PHP Fatal error: Call to undefined function mysql_connect()

解决PHP Fatal error: Call to undefined function mysql_connect()

PHPz
PHPzOriginal
2023-08-25 22:27:221655browse

解决PHP Fatal error: Call to undefined function mysql_connect()

Solution to PHP Fatal error: Call to undefined function mysql_connect()

When using PHP to develop websites, we often encounter various errors and exceptions. Among them, PHP Fatal error: Call to undefined function mysql_connect() is a relatively common error. This error usually occurs when we use PHP to connect to a MySQL database, but forget to enable support for MySQL in the PHP configuration file. In this article, we will discuss how to resolve this error and give some code examples to demonstrate the workaround.

First of all, we need to understand the cause of this error. After PHP 5.5.0, the MySQL extension was deprecated and removed in PHP 7.0.0. Therefore, if you are using PHP version 7.0.0 or higher and try to use the mysql_connect() function to connect to the MySQL database, the above error message will appear. Instead, we should use mysqli or PDO extension to connect to MySQL database.

The first step to solve this problem is to confirm that you are using PHP version 7.0.0 or higher. You can check the PHP version by running php -v on the command line or using echo PHP_VERSION; in the code.

Once the PHP version is confirmed, we can take the following two methods to solve this error.

Method 1: Use the mysqli extension to connect to the MySQL database

The sample code is as follows:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";

// 创建连接
$conn = new mysqli($servername, $username, $password, $database);

// 检测连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

echo "连接成功";
?>

In the above code, we use the mysqli extension to connect to the MySQL database. First, we declare the relevant connection parameters, including server name, user name, password and database name. Then, use new mysqli() to create a mysqli object, and the parameters are server name, user name, password and database name. Next, we use $conn->connect_error to detect whether the connection is successful. If the connection fails, an error message is output; if the connection is successful, "Connection successful" is output.

Method 2: Use PDO extension to connect to the MySQL database

The sample code is as follows:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功";
} catch(PDOException $e) {
    echo "连接失败: " . $e->getMessage();
}
?>

In the above code, we use the PDO extension to connect to the MySQL database. First, we declare the relevant connection parameters, including server name, user name, password and database name. Then, use new PDO() to create a PDO object. The first parameter is the connection string, which contains the server name and database name. The second parameter is the user name, and the third parameter is the password. Next, we use $conn->setAttribute() to set the error mode of the PDO object and set it to throw an exception. Finally, use a try-catch block to capture exceptions that may occur in the connection. If the connection fails, an error message is output; if the connection is successful, "Connection successful" is output.

Summary:

In this article, we discussed how to solve PHP Fatal error: Call to undefined function mysql_connect() error. We pointed out that the reason for this error is that the MySQL extension was removed in PHP7.0.0 version, and gave two solutions, namely using the mysqli extension and the PDO extension to connect to the MySQL database. Through the above code examples, I hope readers can successfully solve this error and successfully connect to the MySQL database.

The above is the detailed content of 解决PHP Fatal error: Call to undefined function mysql_connect(). 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