在php中自动关闭数据库连接? [英] Automatic db connection close in php?
问题描述
我的 config.php 文件中有我的一个网络应用程序.
I have following in my config.php file for one of my web app.
//db settings
$host = 'localhost'; //database location
$user = 'example'; //database username
$pass = 'example12'; //database password
$db_name = 'example'; //database name
//create db connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
//sets encoding to utf8
mysql_query("SET NAMES utf8");
我将 config.php
包含在我应用程序的所有页面顶部(代码的第一行).
I am including config.php
in all pages of my application at top (1st line of code).
我想知道 PHP/MYSQL 会自动保持连接打开和关闭还是我需要显式调用它们.连接正在自行打开,因为我正在正确执行所有基于数据库的任务.但不要在任何地方关闭连接,这是一个高流量网站.
I want to know that will PHP/MYSQL maintain connection open and close automatically or I need to explicitly call them. Connection is opening by itself, as I am doing all db based tasks properly. But not closing connection anywhere, this is a high traffic website.
如有不对之处请指正.
谢谢!
推荐答案
脚本终止时连接将自动关闭.
The connection will close automatically upon termination of the script.
The notes on mysql_connect()
state that:
脚本执行结束后,服务器的链接将立即关闭,除非通过显式调用 mysql_close()
提前关闭.
The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling
mysql_close()
.
这篇关于在php中自动关闭数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!