开发者

Increment a column in MySQL

开发者 https://www.devze.com 2023-02-19 06:38 出处:网络
Is there a way in Zend to increment an integer, held in a MySQL column, by 1? Thanks edit: My current code is like:

Is there a way in Zend to increment an integer, held in a MySQL column, by 1?

Thanks

edit:

My current code is like:

$row = $this->find($imageId)->current();
开发者_JAVA技巧$row->votes = // THIS IS WHERE I WANT TO SAY INCREMENT
$row->save();


Yes there is. Here's an example using products and incrementing a quantity field:

$table     = 'products'; 
$data      = array('prd_qnty' => new Zend_Db_Expr('prd_qnty + 1')); 
$where[] = $db->quoteInto('pr_id = ?', $this->pr_id); 
$db->update($table, $data, $where);


votes++ does not protect against race conditions from other requests, that's why it is desirable to have a database solution.

For example: imagine two requests come in at almost the same time

1st request loads object - votes is 500
2nd request loads object - votes is 500
1st increments value in memory - votes is 501
2nd increments value in memory - votes is 501
1st saves to db - votes is 501
2nd saves to db - votes is 501 (should be 502)


Might I offer the following suggestion

$row->votes++;
0

精彩评论

暂无评论...
验证码 换一张
取 消