Snehanshu Snehanshu - 5 months ago 8
SQL Question

How to increment value in MySQL with PHP mysqli

I have a integer column in a MySQL table called

col1
. Now, what I need is to increase its value by some number say 1 (maybe 2, 3 or anything). i.e. If it was already holding value of 10, now I want it to become 11. I know, I can do it by first selecting the original value, increment it with PHP and then update the value. But I wanted to know if there is a way through which I don't have to select the previous value to increment it.

Note: I am using PHP with mysqli in OOP style.

Answer

This can be done very simply, just execute a query like this

$sql = "UPDATE tablename SET col1=col1+1 WHERE key=99";

Or any value you like

$sql = "UPDATE tablename SET col1=col1+3 WHERE key=99";