如何防止在多次重新加载/刷新页面时超过 max_user_connections?
我正在使用名为 getfreehosting.co.uk 的免费网络托管服务.max_user_connections
是 5.每次我的页面加载时,它做的第一件事就是调用一个 load.php
从我的数据库中获取一个条目.我的问题是,如果我多次重新加载或刷新页面,则会出现以下错误:
I am using a free web hosting service called getfreehosting.co.uk. The max_user_connections
is 5. Every time my page loads, the first thing it does is invoke a load.php
that fetches an entry from my database. My problem is that if I reload or refresh the page too many times, I get the following error:
警告:mysql_connect() [function.mysql-connect]:用户 getfh_11654008已经有超过 'max_user_connections' 个活动连接/home/vol9/getfreehosting.co.uk/getfh_11654008/htdocs/experiment/load.php在第 3 行无法连接:用户 getfh_11654008 已经超过'max_user_connections' 活动连接
Warning: mysql_connect() [function.mysql-connect]: User getfh_11654008 already has more than 'max_user_connections' active connections in /home/vol9/getfreehosting.co.uk/getfh_11654008/htdocs/experiment/load.php on line 3 Could not connect: User getfh_11654008 already has more than 'max_user_connections' active connections
我知道我使用的是免费网络主机,所以我可能无法增加 max_user_connections
.反正我不应该这样做,因为我只需要在任何给定时间运行我的页面的一个实例,所以理论上 max_user_connections = 5
对我的目的来说已经足够了.起初我认为问题一定是我的 load.php
被调用了太多次,导致太多 sql_connections
没有关闭.但是,我在适当的地方调用了 sql_close()
,但仍然设法超过了 max_user_connections
.
I understand that I'm using a free web host, so I may not be able to increase max_user_connections
. I shouldn't have to anyway because I only need to have one instance of my page running at any given time, so in theory max_user_connections = 5
is more than enough for my purposes. At first I thought the issue must be that my load.php
was being called too many times, resulting in too many sql_connections
that aren't closed. However, I call sql_close()
at the appropriate places but still manage to exceed max_user_connections
.
这是我的load.php
:
<?php
$link = mysql_connect(MYSQL_HOSTNAME, MY_USERNAME, MY_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(MY_DB, $link);
$result = mysql_query(SELECT ONE ENTRY);
if($r = mysql_fetch_assoc($result)) {
print json_encode($r); // convert data to json and return it
mysql_close($link);
}
else {
mysql_close($link);
die("Error: something went wrong");
}
?>
出了什么问题,我该怎么办?
What's wrong and what should I do?
答案:
您可以查看两件事:
ANSWER:
Two things you can look into:
- mysql_close() - 系统可能正在等待几秒钟关闭连接,因为您没有告诉它.(即:计算机不会立即关闭门,直到您告诉它或直到所有事情都说完并完成之后).
$connection = mysql_connect();mysql_close($connection);
$connection = mysql_connect(); mysql_close($connection);
确保在连接时使用该变量...
Make sure to use the variable when connecting...
- 你可以试试php的mysql持久连接.本质上是与数据库的永久连接……这并不理想,但它也可以工作.它的语法几乎与 mysql_connect() 相同:
$connection = mysql_pconnect("localhost","mysql_user","mysql_pwd");
$connection = mysql_pconnect("localhost","mysql_user","mysql_pwd");
如果您有兴趣,我可以为您提供更好的 php/mysql 开发环境(免费,没有任何义务)... 100% 您的选择,只是想帮助您.
If your interested I may be able to provide you a better php/mysql development environment (for free, with no obligations of any kind)... 100% your choice, just trying to help you out.
我不明白为什么这里的人坚持要告诉其他人使用 PDO,但却无助于解决问题……人们会使用他们想要的任何东西,直到他们学会更好的方法来做到这一点.打败 PDO 马不会让人们更快地采用它...
I don't understand why people on here insist on telling others to use PDO, but don't help solve the problem... people are going to use whatever they want to until they learn a better way to do it. Beating the PDO horse isn't gonna make people adopt it faster...