简体   繁体   中英

query “select for update” in codeigniter

I have query = SELECT @condi:=id FROM table WHERE id>1 LIMIT 1 for UPDATE; UPDATE table set aa="ok" WHERE id=@condi SELECT @condi:=id FROM table WHERE id>1 LIMIT 1 for UPDATE; UPDATE table set aa="ok" WHERE id=@condi

I used $this->db->query(query) but codeigniter info error "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 'update table set aa="ok" where id=@condi' at line 1" Miss ";" I use phpmyadmin execuse this query, it's result row correct not error query

maybe core codeigniter error can you help me execuse my query in codeigniter,thanks

You could try to use query builder to prevent SQL error:

$query = $this->db->select()
            ->from('table')
            ->where('id >', 1)
            ->limit(1)
            ->get_compiled_select();
$data = $this->db->query("{$query} FOR UPDATE")->row_array();

$this->db->where('id', $condi)->update('table', ['aa'=>'ok']);

Because the Codeigniter 3 builder doesn't support FOR UPDATE , so I just use compiler to rebuild query.

Codeigniter get_compiled_select()

yidas/codeigniter-model - Pessimistic Locking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM