note 60416 deleted from function.mysql-affected-rows by mazzanet
| From: | mazzanet@php.net | Date: | Fri, 06 Jan 2006 10:34:21 +0000 |
| Subject: | note 60416 deleted from function.mysql-affected-rows by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-101299@lists.php.net to get a copy of this message | ||
Note Submitter: radek at pinkbike dot com
----
The code below by relsqui clears up one issue but forms another one depending on your notion of
"SUCCESS" of an update query.
<?php
// ALSO RIGHT
if (mysql_affected_rows() != -1) {
// Success!
if (mysql_affected_rows() == 0) {
// . . . but nothing has changed.
}
} else {
// Failure.
}
?>
If you issue an update query for a record that does NOT exist in the table, then the code above will
return SUCCESS "but nothing changed". Theoretically correct...nothing was updated, but not
really SUCCESS form many points of view.
This is the same status as if you updated an existing record with its current or unchanged values.
Nothing changed.
In the one case your databae contains the record, and in the other it does not. A little confusing.
If you are using this function to verify that an update occured and a record exists, you can create
an extra tinyint column in your table and on every update set touch=touch XOR 1, which will
gurantee that you never get a 0 affected rows from an actual update.
With that in mind you can then assume this...
<?php
$affectedrows=mysql_affected_rows();
if ($affectedrows> 0) {
// Success!
// You updated somthing.
}
} else if ($affectedrows== 0){
// The record you are tring to update does not exist
} else {
// Failure (db problem).
}
?>
The other atomic way to do an update and be clear as to what happened is to use transactions.
Someone mentioned doing this update using a count=count+1 field. NOTE that once you reach the
maximum count for your column type, (tinyint:255) then this will fail as there is no roll over. Yes
you could make a larger type but you always have a limit with the count.
PS. No, there are no concurency issues with the xor method