php - 来自 PHP (CodeIgniter) 的 MySQL 调用无法正常工作

标签 php mysql sql codeigniter

我是 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/

相关文章:

sql - 过滤postgresql查询结果

mysql子查询返回多行

mysql - SQL - 将值传递给同一表上的嵌套选择查询而不选择值

php - 如何在Wordpress中添加Google的utm_source变量?

php - exec() 通过命令行而不是网络运行

php - 将 URL 链接写入 Elgg 数据库

php - 如何在另一个网站上显示Liferay数据库中存储的数据?

sql - 如何使这个 Mysql 查询工作?

mysql - MySQL中的语法错误,但是在创建ROLE时语法正确

php - mysql时间如何转换