dongmiao520892 2018-08-11 07:54
浏览 58

如何转换此codeigniter查询以选择更新

Lost update is a common MySQL issue and to solve it the select query should be edited to be a select for update query like this

select * from users where id = 1 FOR UPDATE

to prevent other users from working on the same row at the same time. I want to do this in codeigniter by editing this query to be a select for update query

public function get_row_data($id)
{

    $this->db->where('id',$id);

    $result = $this->db->get('users');

    if($result && $result->num_rows() > 0)
    {
        return $result->row();
    }
    else
    {
        return false;
    }
}

I can use the above simple query but prefer if the model query could be converted.

Thanks

  • 写回答

1条回答 默认 最新

  • doushou9028 2018-08-11 11:55
    关注

    It Seems You want to acquire Row Level Locking in Mysql and codeigniter query builder doesn't allow to manipulate its method to produce custom sql query

    $this->db->trans_begin();
    $this->db->query('you query goes here');
    // other task .....
    if ($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
    } else {
        $this->db->trans_commit();
    }
    
    评论

报告相同问题?