php-更新查询在mysql中引发错误

这个问题已经在这里有了答案:            >            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 1

UPDATE mostread SET read = read+1 WHERE open_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);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...