note 20910 modified in function.mysql-affected-rows by danbrown

From: Date: Tue, 28 Apr 2009 01:53:00 +0000
Subject: note 20910 modified in function.mysql-affected-rows by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153887@lists.php.net to get a copy of this message
mysql_affected_rows() reports on the number of rows affected by an in-place operation on the database, but mysql_num_rows() returns the number of rows in a MySQL record set (which is held by PHP after MySQL has generated it). This means that if you can do <?php $a = mysql_query("SELECT ..."); $b = mysql_query("SELECT ..."); if (mysql_unm_rows($a) > mysql_num_rows($b)) print "a is larger"; else print "b is larger"; ?> ... but this does not make sense for the operations supported by mysql_affected_rows(), which reports on the status of the database connection as a whole. Particularly note this: <?php $query = "UPDATE ..."; mysql_query($query); print mysql_affected_rows(); // more than 0 mysql_query($query); // same query twice print mysql_affected_rows(); // 0. ?> .. this is because the 2nd time you execute the identical query, all the rows are already updated so no rows are affected the 2nd time. I hope this clears up why mysql_num_rows() and mysql_affected_rows() are fundamentally different --was-- mysql_affected_rows() reports on the number of rows affected by an in-place operation on the database, but mysql_num_rows() returns the number of rows in a MySQL record set (which is held by PHP after MySQL has generated it). This means that if you can do $a = mysql_query("SELECT ..."); $b = mysql_query("SELECT ..."); if (mysql_unm_rows($a) > mysql_num_rows($b)) print "a is larger"; else print "b is larger"; ... but this does not make sense for the operations supported by mysql_affected_rows(), which reports on the status of the database connection as a whole. Particularly note this: $query = "UPDATE ..."; mysql_query($query); print mysql_affected_rows(); // more than 0 mysql_query($query); // same query twice print mysql_affected_rows(); // 0. .. this is because the 2nd time you execute the identical query, all the rows are already updated so no rows are affected the 2nd time. I hope this clears up why mysql_num_rows() and mysql_affected_rows() are fundamentally different http://php.net/manual/en/function.mysql-affected-rows.php

« previous php.notes (#153887) next »