I have to to react fast on a status change in my database table "soundboard". If the status is changed to 2, I want that my application will notice it.
So whats better, I created 2 examples one for an infinite loop and one for an ajax / setInterval script.
First the infinite loop:
<?php
// Settings & Connection
set_time_limit (0);
$data = array(
"id" => "",
"file" => ""
);
$db = new mysqli('p:localhost', 'username', 'password', 'database');
// Query 2 DB
$sql = "SELECT id, name, file FROM soundboard WHERE status = 2 LIMIT 1;";
while(TRUE) {
$result = $db->query($sql);
if($result->num_rows > 0) {
// Fill $data here
echo json_encode($data);
break;
}
unset($result);
sleep(5);
}
$db->close();
unset($db, $data, $sql);
exit;
?>
Or is this better for the perfomance?
<?php
// Settings & Connection
$data = array(
"status" => 0,
"id" => "",
"file" => ""
);
$db = new mysqli('localhost', 'username', 'password', 'database');
// Query 2 DB
$sql = "SELECT id, name, file FROM soundboard WHERE status = 2 LIMIT 1;";
$result = $db->query($sql);
if($result->num_rows > 0) {
// Fill $data here
}
$db->close();
echo json_encode($data);
unset($db, $data, $sql);
exit;
?>
I hope you can help me and understand what I am trying to do.