在PHP中关闭自动数据库连接?

在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");

I am including config.php in all pages of my application at top (1st line of code).

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.

Please correct me if I am wrong anywhere.

Thanks!

我的config.php文件中有以下内容供我的某个网络应用使用。 p>

  // db settings 
 $ host ='localhost';  //数据库位置
 $ user ='example';  //数据库用户名
 $ pass ='example12';  //数据库密码
 $ db_name ='example';  //数据库名称
 
 //创建数据库连接
 $ link = mysql_connect($ host,$ user,$ pass); 
 mysql_select_db($ db_name); 
 
 //将编码设置为utf8 
  mysql_query(“SET NAMES utf8”); 
  code>  pre> 
 
 

我在应用程序的所有页面中都包含 config.php code>(第1行) 代码)。 p>

我想知道PHP / MYSQL会自动保持连接是否打开和关闭,或者我需要显式调用它们。 连接是自行打开的,因为我正在执行所有基于数据库的任务。 但不是在任何地方关闭连接,这是一个流量很大的网站。 p>

如果我在任何地方都错了,请纠正我。 p>

谢谢! p> div>

The connection will close automatically upon termination of the script.

The notes on mysql_connect() state that:

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().