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.
Add a comment
|
4 Answers
I have a table with a single record used for multiple counters.
Manually create a table called
countswith a singleIntcolumn, namedmyCount.
(Later, add more Int columns for additional counters.)
ie.,CREATE TABLE counts (myCount int);manually insert a single record with a value of
0for columnmyCount.
(Never add more records, just use this one for all your counters.)
ie.,INSERT INTO counts (myCount) VALUES (0);
Then, run this code each time you want to increment counter myCount by one:
<?php
require_once('connect.php');
$cn = new mysqli($servername, $username, $password, $dbname);
if(!$cn->connect_error){$cn->query("UPDATE counts SET myCount=myCount+1");}
?>
connect.phpis a simple connection info file.- This code (intentionally) produces no output, even if there's a problem.