Is it possible in MySQL+PHP to increase an INT value and return the new value within one query?
$sql = mysql_query("UPDATE table SET number=number+1 WHERE id='uniqid'");
$updated_number = ???
Or I need to post another query?
SELECT number FROM table WHERE id='uniqid'
Simple answer – no, it’s not possible.
Longer answer, yes, if you use a stored procedure that increments the value for the specified ID, retrieves the new value and returns it.
I’ve just tested this under MySQL 5.1.59:
Usage:
If multiple simultaneous accesses are possible you may wish to
LOCKthe table first to ensure the operation’s atomicity – MySQL apparently doesn’t allow stored procedures to lock tables themselves.