我需要使用mysql_close(connection)吗? [英] Do I need to use mysql_close(connection)?

查看:248
本文介绍了我需要使用mysql_close(connection)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在db.php中有以下代码可以连接到我的数据库.

I have the following code in db.php to connect to my DB.

<?php
$DB_HOST        = "localhost";
$DB_NAME        = "db";
$DB_USER        = "user";
$DB_PASSWORD        = "pass";

$con = mysql_connect($DB_HOST, $DB_USER, $DB_PASSWORD);

if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db($DB_NAME , $con);
?>

在我的其他脚本中,我使用以下命令将其包括在内:

In my other script I include it using:

include("db.php");

在某些情况下,我会收到ff错误:

In some cases I receive the ff error:

[2012年3月10日10:47:20] PHP警告:mysql_connect()[function.mysql-connect]:用户db_user在/home/user/public_html/sc/中已具有多个"max_user_connections"活动连接第8行上的db.php

[10-Mar-2012 10:47:20] PHP Warning: mysql_connect() [function.mysql-connect]: User db_user already has more than 'max_user_connections' active connections in /home/user/public_html/sc/db.php on line 8

现在,我想知道是否需要关闭连接:

Now, I am wondering if I need to close the connection like:

<?php
include("db.php");

//other stuff here

mysql_close($con);
?>

顺便说一句,我的MySQL配置中的max_connections值为100.

BTW, I have a value of 100 max_connections in my MySQL config.

我还研究了持久连接,我相信上面的代码不是持久连接.

I also research about persistent connection and I believe my code above is not a persistent connection.

推荐答案

否,如果在脚本末尾将其关闭,这将无济于事. mysql_close()仅在您希望在结束脚本之前释放资源时有用,因为一旦脚本执行结束,连接就会关闭

No, this won't help you if you close it at the end of the script. mysql_close() is just useful in case you want to free up resources before you end your script, because your connection is closed as soon as execution of the script ends

这篇关于我需要使用mysql_close(connection)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆