php - UPDATE sql query and get the updated field in one single query -
i'm trying update value of column using codeigniter query function this:
$this->db->query("update table set val = val + 1 name = 'xxxxx'); is there way result of update in same query function? have select query in order , it's dangerous because of amount of users application managing.
if there query in between update , select, result not correct.
thanks!
use transaction , update. example zend, similar kind of db accessing thing:
$db->begintransaction(); $val = $db->select()->forupdate()->from('table', 'val')->orderby('val desc')->limit(1)->query()->fetchcolumn(); $db->update('table', 'val = '.($val+1), 'name = "xxx"'); $db->commit() the for-update transaction prevents query interfering.
learn more update here: http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html
and codeigniter transactions here: http://ellislab.com/codeigniter/user-guide/database/transactions.html (thanks @nanne that)
Comments
Post a Comment