I want to increment a field value safely using php and mysql.
What type of table/field must I use?
Is there a minimum version of MySQL I must use?
What's the sql code for this, safe transaction for MySQL?
By what type of "table" I assume you mean storage engine. Anything that supports mutations (i.e. not "archive" or "black hole")
Any numeric field will do (tinyint, int, float, etc). That said, there's no special PHP code, just the SQL for incrementing the desired field:
UPDATE table SET field = field + 1 WHERE [...]
If you want a transaction, then pack the above query into a transaction. As for MySQL version, I agree with @hsz - use the most current version possible.
If you are talking about primary key then set id
column as primary
and auto_increment
.
Increasing field is looking like that:
UPDATE table SET field = field + 1 WHERE id = 9
About MySQL version - use the newest you can. ;)> 5.0
will be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With