mysql 连接在一段时间后关闭? (PHP)

发布于 2024-08-08 19:44:35 字数 180 浏览 2 评论 0原文

我有一个需要几分钟才能执行的脚本。 mysql 连接在开始时建立(mysql_connect),然后是一些查询的循环(mysql_query)。该循环持续很长时间,几分钟后脚本会抛出一条警告,指出 mysql 服务器已消失(提供的参数不是有效的 MySQL 结果资源)

连接是否在执行一定时间后自动关闭,即使查询是制作并获取结果?

I have a script that takes several minutes to execute. The mysql connection is established at the beginning (mysql_connect), then a loop with some queries follows (mysql_query). That loop lasts very long, and after a few minutes the script throws a warning that the mysql server has gone away (Supplied argument is not a valid MySQL result resource)

Does the connection close automatically after a certain time of execution, even though queries are made and results are fetched?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2024-08-15 19:44:35

“提供的参数不是有效的 MySQL 结果资源”意味着 mysql_...() 函数期望您传递结果资源,但您确实传递了其他内容,例如

$result = false;
$row = mysql_fetch_array($result, MYSQL_ASSOC);
// => Supplied argument is not a valid MySQL result resource

那么,为什么 $result 为 false >?大多数情况下,这是因为之前的查询失败并且脚本中没有错误处理。每个查询都可能失败,您无法采取任何措施来防止这种情况发生。因此,您总是需要某种错误处理,以便您的脚本在没有错误(但出现错误情况)时不会尝试处理结果。最简单的错误处理是让脚本在发生错误时停止。

$sql = 'SELECT x,y,z FROM ...';
$result = mysql_query($sql, $mysql) or die(mysql_error());

如果出现错误,mysql_query() 返回false。在这种情况下,执行or之后的语句,即如果查询失败,php将打印MySQL的最后一条错误消息,然后退出。
不过,您可能想要实现更高级的错误处理例程...
例如,当 PDO 对象设置为 PDO::ERRMODE_EXCEPTION 时,只要发生错误,就会引发异常。与(简单的)返回值相比,更难忽略这一点。

编辑:这是对原始答案的完全重写。第一个建议是查看 的值wait_timeout

"Supplied argument is not a valid MySQL result resource" means that a mysql_...() function expects you to pass a result resource but you did pass something else, e.g.

$result = false;
$row = mysql_fetch_array($result, MYSQL_ASSOC);
// => Supplied argument is not a valid MySQL result resource

So, why would $result be false? Amost always it's because the previous query failed and there is no error handling in the script. Each and every query can fail, there's nothing you can do to prevent that from ever happening. Therefore you always need some kind of error handling so that your script doesn't try to process the result when there is none (but an error condition). The simplest error handling is to let the script stop whenever an error occurred.

$sql = 'SELECT x,y,z FROM ...';
$result = mysql_query($sql, $mysql) or die(mysql_error());

mysql_query() returns false if there was an error. In that case the statement after or is executed, i.e. if the query failed php will print MySQL's last error message and then quit.
You might want to implement a somewhat more advanced error handling routine though...
E.g. when an PDO object is set to PDO::ERRMODE_EXCEPTION an exception is thrown whenever an error occurs. It's a bit harder to miss that compared to a (simple) return value.

edit: this is a complete rewrite of the original answer. The first suggestion was to look at the value of wait_timeout.

风轻花落早 2024-08-15 19:44:35

您可能遇到了 php 设置的超时
最大执行时间
设置时间限制

you're probably hitting a time out set by php
Max Execution Time
Set Time Limit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文