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);
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 = 'localhost'; $dbuser = 'username'; $dbpass = 'password'; $connection = mysql_connect($dbhost, $dbuser, $dbpass); // 如果无法连接到数据库,则显示错误信息 if(! $connection ) { die('无法连接: ' . mysql_error()); } echo '连接成功'; // 关闭数据库连接 mysql_close($connection); ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
