Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I increment a column in mysql using prepared statements?

Tags:

php

Without prepared statements one can simply use "SET column=column+1" to increment a column. However I can't seem to get this working using prepared statements. If it's even possible, what am I doing wrong here?

<?php

$mysqli = new mysqli('host', 'user', 'pass', 'database');

$query = "UPDATE forums_topics SET posts=? WHERE id=?";

if ($stmt = $mysqli->prepare($query)) {
    $stmt->bind_param('ii', 'posts' + 1, 1);

    $stmt->execute();

    $stmt->close();
}
like image 553
CrazeD Avatar asked Dec 12 '22 07:12

CrazeD


1 Answers

 $query = "UPDATE forums_topics SET posts=posts+? WHERE id=?"
like image 164
勿绮语 Avatar answered May 25 '23 17:05

勿绮语