我在 PHP 中使用下面的 sleep mysql 查询。
select sleep(50);
需要 50 秒才能完成。 PHP 页面正在加载直到 50 秒。这里我想在PHP页面中抛出异常。如何才能实现这一目标?
最佳答案
如果你想在30秒后停止脚本,可以使用set_time_limit(30)
,并且可以使用register_shutdown_function()
来处理由于时间到期而退出的脚本:
<?php
set_time_limit(30);
$script_finished = false;
register_shutdown_function('is_finished');
function is_finished(){
global $script_finished;
if(!$script_finished){
//Script got interrupted
echo "Oh, this script takes too much to complete!";
}
}
//Your code here
//...
//...
sleep(50); //For example
//Right before ending of php tag, script has ended
//Don't through is_finished function
$script_finished = true;
?>
关于php - 如何在 PHP 中处理 mysql sleep 查询的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38734563/