0
$query = "update place1 set status=2 where car='$wait_value'";
$result=mysqli_query($connect,$query);

Need a ten minutes delay here, how's that possible?

$query2 = "update place1 set status=1 where car='$wait_value'";
result2=mysqli_query($connect,$query2);
1729i
  • 57
  • 1
  • 5
  • You are looking for a job queue system. – arkascha Feb 17 '17 at 13:26
  • thanks, but it's not possible to use sleep() for such long situation, do you have any other solution? – 1729i Feb 17 '17 at 13:26
  • job queue? I don't know what you meant. I just want to make a time lag of around ten minutes after updating the first statement. – 1729i Feb 17 '17 at 13:28
  • Why do you say it's not possible? Does `sleep(600)` do something different? If so, you'll need to post a [mcve] so we can see what's happening. – Toby Speight Feb 17 '17 at 13:29
  • @TobySpeight Sorry, I didn't meant that. It'll make a long time to run my php file and will get expired. – 1729i Feb 17 '17 at 13:32

2 Answers2

2

You can sleep, but if it's a web app that's not going to work. Try a MySQL event instead:

CREATE EVENT place_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 10 MINUTE
DO
    update place1 set status=1 where car=?

Note that I've used a prepared statement here to bind a parameter to ?; the code you have (using string concatenation) is vulnerable to SQL injection attacks.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • When I invoke my php file, the first two statements will execute in normal way, and my need is to run the second set of statements after a ten minutes delay. – 1729i Feb 17 '17 at 13:37
  • yes, exacty what this does. Replace your simple update statement (the second one) with this – e4c5 Feb 17 '17 at 13:39
  • yes, I done this. But still it's exceeding the maximum execution time. – 1729i Feb 17 '17 at 13:52
  • Well if you schedule a task you need to remove the sleep! – e4c5 Feb 17 '17 at 13:59
  • glad to have been of help. All the best with your project – e4c5 Feb 17 '17 at 14:23
2

Sleep is a VERY bad idea. Client browser would have to wait 10 minutes to finish request!!!

In my opinion it's not possible to do it like you want to.

You should create another script which queries database and checks if there is new data (and on successful fetch does the job). This script should be run by cron every 10 minutes.

How to create cron job using PHP?

Community
  • 1
  • 1
NID
  • 3,238
  • 1
  • 17
  • 28