简体   繁体   中英

How to handle exception for mysql sleep queries in PHP?

I am using below sleep mysql query in PHP.

select sleep(50);

It takes 50 seconds to complete. PHP page is lading till that 50 seconds. Here I want to throw exception in PHP page. 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:

<?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;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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