我需要使用mysql_close(连接)吗?
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");
In some cases I receive the ff error:
[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);
?>
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.
我在db.php中有以下代码连接到我的数据库。 p>
在我的其他脚本中,我使用以下方法包含它: p>
在某些情况下,我收到ff错误: p>
[10 -Mar-2012 10:47:20] PHP警告:mysql_connect()[function.mysql-connect]:用户db_user已经在/home/user/public_html/sc/db.php上有超过'max_user_connections'的活动连接 8 p>
现在,我想知道是否需要关闭连接,如: p>
BTW,我有一个值 1 我的MySQL配置中的00 max_connections。 p>
我还研究了持久连接,我相信上面的代码不是持久连接。 p>
div>&lt;?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('无法连接:'。mysql_error());
}
mysql_select_db( $ DB_NAME,$ con);
?&gt;
code> pre>
include(“db.php”);
code> pre>
&lt;?php
include(“db。 php“);
//这里的其他东西
mysql_close($ con);
?&gt;
code> pre>
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
If you don't close your connections, they will stay open and take up precious resources on the server. I guess there's there the security point too, you don't want to risk someone getting a hold of the connection.
I prefer to put my database in a class and use __construct to create the connection, and __destruct to close the connection. If your unfamiliar with classes. The __construct and __destruct gets called automatically when you create and destroy a class.
Edit:
There was originally meant to be an example. But I have a basic but working mysql class here https://stackoverflow.com/a/9651249/1246494.
It shows the usage of mysql_close and how I was trying to relate it to the class destructor. The point was, any network connection should be closed, whether your database is on a remote server, or localhost.