Reputation: 15
how can I increment a value?
$app->db->update('videos', array(
'views' => 'views + 1'
), array(
'id' => $id
));
It doesn't work in many ways I tried.
Upvotes: 1
Views: 1514
Reputation: 192
A better approach could be to use the method executeUpdate()
$app->db->executeUpdate("UPDATE videos SET views=views+1 WHERE id=?", array($id));
The API is working but marked deprecated. Use executeStatement()
instead of executeUpdate()
Upvotes: 4
Reputation: 3912
You have to ask the db for the current value of views
first, then increment the value and save the new value into db.
$result = $app->db->fetchAssoc('SELECT views FROM videos WHERE id = ?', array($id));
$result['views']++;
$app->db->update('videos', array('views' => $result['views']), array('id' => $id));
Upvotes: -1