note 60416 added to function.mysql-affected-rows

From: Date: Fri, 06 Jan 2006 09:03:43 +0000
Subject: note 60416 added to function.mysql-affected-rows
Groups: php.notes 
Request: Send a blank email to php-notes+get-101289@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-affected-rows.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60416 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60416&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60416&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60416&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60416&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60416&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60416&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60416&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60416&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#101289) next »