这个问题已经在这里有了答案: > Syntax error due to using a reserved word as a table or column name in MySQL 1个
我有一个名为“ mostread”的表,其中有2列open_id(int)和read(int).现在的问题是,如果表中已经存在“ open_id”,那么我需要为每次单击更新“ read”,否则我需要插入新行,并从控制器中获取“ open_id”并读取= 1.我在模型中使用以下代码,该代码正确地插入了新行,但是第二次单击它会引发如下错误.
发生数据库错误
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 ‘read = read+1 WHERE
open_id
= ‘193” at line 1UPDATE
mostread
SET read = read+1 WHEREopen_id
= ‘193’Filename: D:/Xampp/htdocs/opunletter/opunletter/application/models/Select.php
Line Number: 52
public function click($id)
{
$query = $this->db->query("SELECT * FROM mostread WHERE open_id='$id'");
$count= $query->num_rows();
if($count > 0) {
$this->db->set('read', 'read+1', FALSE);
$this->db->where('open_id', $id);
$this->db->update('mostread');
$data = array(
'open_id' => $id,
'read' => '1'
);
$this->db->insert('mostread', $data);
return TRUE;
}else{
return FALSE;
}
}
解决方法:
尝试添加反引号arround在mysql中读取其保留的keyword
$this->db->set('`read`', '`read` + 1', FALSE);