Interval in PHP execution

Asked

Viewed 53 times

0

I have made a code in PHP that runs 3 querys, every query displays a message, I would like to know how to give an interval of 800ms after the message to run the next query, for example:

<?php
$sql_1 = "SELECT * FROM `tbl` WHERE `user_id` = '1';";
$query_1 = mysqli_query( $conexao, $sql_1 );
$count_1 = mysqli_num_rows( $query_1 );
echo $count_1 . "<br>\n";

// INTERVALO DE 800ms

$sql_2 = "SELECT * FROM `tbl2` WHERE `user_id` = '1';";
$query_2 = mysqli_query( $conexao, $sql_2 );
$count_2 = mysqli_num_rows( $query_2 );
echo $count_2 . "<br>\n";

// INTERVALO DE 800ms

$sql_3 = "SELECT * FROM `tbl3` WHERE `user_id` = '1';";
$query_3 = mysqli_query( $conexao, $sql_3 );
$count_3 = mysqli_num_rows( $query_3 );
echo $count_3 . "<br>\n";

// INTERVALO DE 800ms

header('Location: page/user/');
exit();
?>
  • 1

    http://php.net/manual/en/function.sleep.php - Delay the execution of the program by a given number of Seconds.

  • 1

    In general this is wrong to do, and this seems to be the case, it seems that it needs another solution, but we have no way of knowing.

1 answer

1


You can use the function sleep() of PHP. It takes as parameter the amount of seconds. But I advise you to search for a way to implement Queues in its application.

$sql_1 = "SELECT * FROM `tbl` WHERE `user_id` = '1';";
$query_1 = mysqli_query( $conexao, $sql_1 );
$count_1 = mysqli_num_rows( $query_1 );
echo $count_1 . "<br>\n";

// INTERVALO DE 800ms
sleep( 0.8 );

$sql_2 = "SELECT * FROM `tbl2` WHERE `user_id` = '1';";
$query_2 = mysqli_query( $conexao, $sql_2 );
$count_2 = mysqli_num_rows( $query_2 );
echo $count_2 . "<br>\n";

// INTERVALO DE 800ms
sleep( 0.8 );

$sql_3 = "SELECT * FROM `tbl3` WHERE `user_id` = '1';";
$query_3 = mysqli_query( $conexao, $sql_3 );
$count_3 = mysqli_num_rows( $query_3 );
echo $count_3 . "<br>\n";

// INTERVALO DE 800ms
sleep( 0.8 );

header('Location: page/user/');
exit();
  • Well commented Kayo, use of queues, good. Thanks for the help and attention paid.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.