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\n");
$this->db->query("UPDATE messages SET `count`=count+1 WHERE `id`=$id");
$this->db->commit();
} else {
echo("not found\n");
}
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 ...\n");
sleep(10);
}
}
当我运行时SendAll()
,结果会从数据库中一一正确检索。后来,当所有记录都更新(count
从 0 到 1)时,我通过 Sequel Pro 更新数据库记录(count
从 1 到 0)。奇怪的是,程序一直显示not found
在睡眠循环中。
但是,如果我 ctrl-c 程序并重新启动。再次找到记录。我的代码有什么问题?
数据库连接设置:
$this->db = new mysqli('127.0.0.1', 'username', 'password', 'sendmsg');
$this->db->autocommit(FALSE);