function GetMessageFromDB(&$id, &$phone, &$message)
{
$found = FALSE;
$stmt = $this->db->prepare('SELECT id, phone, message FROM messages WHERE count <= 0 LIMIT 1');
$stmt->execute();
$stmt->bind_result($id, $phone, $message);
$found = $stmt->fetch();
$stmt->close();
echo("GetMessageFromDB: ... ");
if ($found) {
echo("found
");
$this->db->query("UPDATE messages SET `count`=count+1 WHERE `id`=$id");
$this->db->commit();
} else {
echo("not found
");
}
return $found;
}
function SendAll()
{
while (true) {
$found = $this->GetMessageFromDB($id, $phone, $message);
while ($found) {
# $this->DoSomething($phone, $message);
$found = $this->GetMessageFromDB($id, $phone, $message);
}
echo("sleep ...
");
sleep(10);
}
}
When I run SendAll()
, results are correctly retrieved from the DB one by one. Later, when all records are updated (count
from 0 to 1), I update the database record via Sequel Pro (count
from 1 to 0). The strange thing is, the program keeps showing not found
in the sleep loop.
However, if I ctrl-c the program and restart. Records are found again. What's wrong in my code?
Database connection setup:
$this->db = new mysqli('127.0.0.1', 'username', 'password', 'sendmsg');
$this->db->autocommit(FALSE);
$found = $stmt->fetchAll();
Try this