我是 PHP(和 CodeIgniter)新手,在尝试简单调用 $this->db->query() 时遇到问题。在我的代码中(请参阅帖子底部),我正在查看数据库表 eventLog 中记录的错误和警告,并将每个错误和警告与已接受的错误消息列表进行比较。如果在该列表中找不到任何消息,那么我想通过设置“ack = 't'”来确认它。但是代码无法成功运行并给出消息:
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
UPDATE eventLog SET ack = 't' WHERE id =
此消息似乎表明“$row['id']”未按我预期的方式工作。它应该给我错误或警告的 ID,但错误表明它没有给出任何内容,或者格式错误。我缺少什么?或者有没有更好的方法可以绕过我的问题?
我的代码:
$acceptedMessages = array("whatever",
"whateverelse"
);
$sql = "(SELECT id, mesg FROM eventLog WHERE level = 'error') UNION
(SELECT id, mesg FROM eventLog WHERE level LIKE 'warn%')" ;
foreach ($this->db->query($sql) as $row) {
foreach ($acceptedMessages as $messagePart) {
$pos = strpos($row['mesg'], $messagePart);
if ($pos !== false) {
continue 2;
}
}
// if we get here the eventLog message didn't match any accepted messages, so
// acknowledge it
$idNum = $row['id'];
$sql3 = "UPDATE eventLog SET ack = 't' WHERE id = " . $idNum;
$res = $this->db->query($sql3);
if (!$res) { echo "Failed to acknowledge error/warning with id " . $row['id'] . " in eventLog"; }
}
最佳答案
$this->db->query($sql)
返回结果对象,但不是数组,因此 foreach 不应该工作。试试这个:
$query = $this->db->query($sql);
foreach ($query->result_array() as $row) {
查看文档以获取更多选项:http://ellislab.com/codeigniter/user-guide/database/results.html
关于php - 来自 PHP (CodeIgniter) 的 MySQL 调用无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593337/