PHP 脚本超时
我有一个用于公告板系统的自定义脚本,它可以计算用户创建的线程数,并相应地更新列。这工作正常,但是对于超过 100,000 个用户,第一次运行时会超时。
我尝试在查询之前添加以下内容,但它仍然超时(500 错误)。
set_time_limit(0);
ignore_user_abort(true);
附加:我在我的 vps 上使用此脚本。
查询:
set_time_limit(0);
ignore_user_abort(true);
$db->write_query("ALTER TABLE `".TABLE_PREFIX."users` ADD `numthreads` int(10) unsigned NOT NULL default '0'");
// load users into an array to count number of threads
$query = $db->simple_select("users", "uid");
while($user = $db->fetch_array($query))
{
$users[$user['uid']] = $user;
}
foreach($users as $user)
{
$query = $db->simple_select("threads", "COUNT(tid) AS threads", "uid = '{$user['uid']}'");
// get total number of threads
$numthreads = intval($db->fetch_field($query, "threads"));
$db->update_query("users", array("numthreads" => $numthreads), "uid = '{$user['uid']}'");
}
I have a custom script for a bulletin board system that counts the number of threads a user has made, and updates a column accordingly. This works fine, however with 100,000+ users, it times out when running it for the first time.
I've tried adding the following before the query, but it still times out (500 error).
set_time_limit(0);
ignore_user_abort(true);
Additional: I'm using this script on my vps.
Query:
set_time_limit(0);
ignore_user_abort(true);
$db->write_query("ALTER TABLE `".TABLE_PREFIX."users` ADD `numthreads` int(10) unsigned NOT NULL default '0'");
// load users into an array to count number of threads
$query = $db->simple_select("users", "uid");
while($user = $db->fetch_array($query))
{
$users[$user['uid']] = $user;
}
foreach($users as $user)
{
$query = $db->simple_select("threads", "COUNT(tid) AS threads", "uid = '{$user['uid']}'");
// get total number of threads
$numthreads = intval($db->fetch_field($query, "threads"));
$db->update_query("users", array("numthreads" => $numthreads), "uid = '{$user['uid']}'");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用这个:
代替:
您还可以编辑 php.ini:
希望这会有所帮助。
Use this:
Inplace of:
You can also edit php.ini:
Hope this helps.
首先,您应该分开您的 ALTER 语句。先执行ALTER,然后再执行其余的操作。如果你有一张大桌子,那么改变桌子的时间可能会很昂贵。您可以使用 phpmyadmin 或通过 shell 手动运行它(更好,因为没有 php 超时)。这将使您能够不超时。
然后从脚本中删除 ALTER 并运行它。
然后使用:
First you should separate your ALTER statement. Execute the ALTER first and then do the rest. Alter table can be expensive in time if you have a big table. You can run it manually, using phpmyadmin or via shell (even better since there's no php timeout). which will give you the ability to not timeout.
Then remove the ALTER from the script and run it.
and then use:
试试这个
try this
人们无法更改最大执行时间的一个常见原因是他们的主机不允许他们这样做。确保他们不会阻止此操作。
A common reason that people are not able to change the max execution time is that their host does not allow them to. Make sure to find out that they do not block this.