PHP-MySQL-How to safely increment MySQL integer field?

I want to safely increase the value of a field using php and mysql.

  • What type of table / field should I use?

  • Is there a minimal version of MySQL that I should use?

  • What is the sql code for this secure transaction for MySQL?

+41
php mysql counter transactions
Jan 09 '10 at 13:50
source share
3 answers

In what type of "table" do I assume that you mean the storage engine. Anything that supports mutations (ie Not an “archive” or “black hole”)

Any number field will do (tinyint, int, float, etc.). However, there is no special PHP code, just SQL to increase the required field:

UPDATE table SET field = field + 1 WHERE [...] 

If you want to complete a transaction, put the above request in the transaction. Regarding the version of MySQL, I agree with @hsz - use the latest version.

+73
Jan 09
source share
— -

If you are talking about a primary key, set the id column as primary and auto_increment .

The increase in the field is as follows:

 UPDATE table SET field = field + 1 WHERE id = 9 

About MySQL version - use the latest version .;)
> 5.0 will be ok.

+7
Jan 09 '10 at 13:54
source share

1. What type of table / field should I use?

-> The type of table depends on what you have planned for your application. It could be Innodb or Myisam. I suggest you use a numeric column so that you can increase / decrease them. DO NOT do this UNKNOWN if you plan to allow negative numbers.

Here are some limits you might find useful when declaring a column length:

 TINYINT (length) - 1 - Integer with unsigned range of 0-255 and a signed range from -128-127 SMALLINT (length) - 2 - Integer with unsigned range of 0-65535 and a signed range from -32768-32767 MEDIUMINT(length) - 3 - Integer with unsigned range of 0-16777215 and a signed range from -8388608-8388607 INT(length) - 4 - Integer with unsigned range of 0-429467295 and a signed range from -2147483648-2147483647 BIGINT(length) - 8 - Integer with unsigned range of 0-18446744 and a signed range from -9223372036854775808-9223372036854775807 

2. Is there a minimum version of MySQL that I should use?

-> Just use auto increment? You make good use of the latest version. I suggest something> 5.2.4 if possible.

3. What is the sql code for this, a safe transaction for MySQL?

-> Sorry, there is currently no answer.

+2
Jan 9 '10 at 14:18
source share



All Articles