简体   繁体   English

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

[英]How to handle exception for mysql sleep queries in PHP?

I am using below sleep mysql query in PHP. 我在PHP中使用以下sleep mysql查询。

select sleep(50);

It takes 50 seconds to complete. 需要50秒才能完成。 PHP page is lading till that 50 seconds. PHP页面一直加载到50秒为止。 Here I want to throw exception in PHP page. 在这里,我想在PHP页面中引发异常。 How can be achieved this ? 如何实现呢?

You can use set_time_limit(30) if you want to stop the script after 30 seconds and you can use register_shutdown_function() to handle the script exiting because of time expiration: 如果要在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;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM