PHP 8.5.0 Alpha 4 available for testing

Voting

: three minus three?
(Example: nine)

The Note You're Voting On

ekalashnikov at gmail dot com
7 years ago
Improved version of using polling to check for slow query,
"KILL #ID" does not work if id of the myqsli link is the same as the one that kills it,
so you have to disconnect from mysqli and connect again with new link to
kill the #ID.
<?php
// Slow SQL Query
$SelectSql = 'SELECT * FROM SLOW_QUERY';
$link = mysqli_connect('localhost','user','pass','database');
mysqli_query($SelectSql, MYSQLI_ASYNC);
$thread_id = mysqli_thread_id($link);
// Ignore user abort so we can kill the query
ignore_user_abort(true);
$MaxTime = 5; // seconds
$Overtime = false;
$StartTime = time();
do
{
// Poll MySQL
$links = $errors = $reject = array($link);
$poll = mysqli_poll($links, $errors, $reject, 0, 500000);
// Check if the connection is aborted and the query was killed
if (connection_aborted()) {
$link_new = mysqli_connect('localhost','user','pass','database');
mysqli_kill($link_new, $thread_id);
$kill = mysqli_kill($link_new, $thread_id);
if (
$kill)
{
die();
}
}
$EndTime = time();
// Check overtime, kill if detected overtime
if ($EndTime - $StartTime > $MaxTime)
{
$link_new = mysqli_connect('localhost','user','pass','database');
mysqli_kill($link_new, $thread_id);
$Overtime = true;
echo
'Error: Query took over '.$Overtime.'.';
}
} while (!
$poll && $Overtime == false);
?>

<< Back to user notes page

To Top