1

I want to execute two queries at once using mysqli_multi_query statement but only the first query is getting executed. Here is my code

<?php
$connection=mysqli_connect("localhost","root","","entries");
$query1="select * from jobs";
$query1exe=mysqli_query($connection,$query1);
while($query1collector=mysqli_fetch_array($query1exe,MYSQLI_ASSOC)) {
    $query2="select sleep(10);
    select 1;";
    mysqli_multi_query($connection,$query2);
}
?>

In the above code there is a delay of 10 seconds. But when I interchange the two select statements as below there is no delay at all which made me conclude that the second query is not getting executed.

<?php
$connection=mysqli_connect("localhost","root","","entries");
$query1="select * from jobs";
$query1exe=mysqli_query($connection,$query1);
while($query1collector=mysqli_fetch_array($query1exe,MYSQLI_ASSOC)) {
    $query2="select 1;
    select sleep(10);";
    mysqli_multi_query($connection,$query2);
}
?>

What is happening? I am using php 5.5.11. I am new to mysqli statements.

  • 1
    See this : http://stackoverflow.com/questions/6102968/php-mysqli-multi-query-asynchronous - mysqli_multi_query returns after the first command has executed, the other commands are still running on the MySQL server. – PaulF Oct 22 '15 at 15:59
  • Why are you using mysql to sleep? You could use PHP's builtin sleep: http://php.net/sleep – Mike Purcell Oct 22 '15 at 16:59
  • Thank you @PaulF the query is being executed in the background. But only once. The new query is not getting executed at all in the next loop (even the first statement is not getting executed in the next loop) – Sandeep Thedarkprince C Oct 22 '15 at 17:00
  • @MikePurcell Actually I am not interested in making the code sleep. I used the sleep for debugging purpose. – Sandeep Thedarkprince C Oct 22 '15 at 17:01
  • @PaulF I will post that as a new question as it is different from what I have asked in the title. – Sandeep Thedarkprince C Oct 22 '15 at 17:04

0 Answers0