如何在PHP中处理mysql睡眠查询的异常?

时间:2016-08-03 04:42:30

标签: php mysql

我在PHP中使用下面的睡眠mysql查询。

select sleep(50);

完成需要50秒。 PHP页面提货到50秒。这里我想在PHP页面中抛出异常。怎么可以实现这个?

1 个答案:

答案 0 :(得分:1)

如果要在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;
?>