这个问题已经在这里有了答案: > 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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。