Home Backend Development PHP Problem How to close MySQL database connection in PHP

How to close MySQL database connection in PHP

Apr 06, 2023 am 09:15 AM

PHP is used with MySQL to retrieve and store data from the database. When the script execution ends, the connection to the database needs to be closed. In this article, we will explain how to close a MySQL database connection in PHP.

Close the MySQL database connection

In PHP, to close the connection to the MySQL database, you must use the "mysql_close()" function. This function accepts a parameter representing the database connection.

For example:

mysql_close($connection);
Copy after login

In this case, $connection is the name of the connection variable.

Below is a complete example showing how to connect to a MySQL database, and how to close the connection.

<?php

// 定义 MySQL 数据库连接变量
$dbhost = &#39;localhost&#39;;
$dbuser = &#39;username&#39;;
$dbpass = &#39;password&#39;;
$connection = mysql_connect($dbhost, $dbuser, $dbpass);

// 如果无法连接到数据库,则显示错误信息
if(! $connection ) {
   die(&#39;无法连接: &#39; . mysql_error());
}

echo &#39;连接成功&#39;;

// 关闭数据库连接
mysql_close($connection);

?>
Copy after login

In the above example, if it cannot connect to the MySQL database, PHP will exit with an error message. If the connection is successful, the message "Connection successful" will be displayed. At the end of the script, close the database connection using the "mysql_close()" function.

Summary

This article explains how to close a connection to a MySQL database in PHP. After using the database, be sure to remember to close the connection to release resources. Otherwise, the connection will consume server resources and may cause performance issues.

The above is the detailed content of How to close MySQL database connection in PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1654
14
PHP Tutorial
1252
29
C# Tutorial
1225
24