mysql_num_rows() VS mysql_affected_rows()
February 4th, 2010 | Php | 3 Comments »Whats is the difference between mysql_num_rows() and mysql_affected_rows()? mysql_num_rows() is used together with select statements while the mysql_affected_rows() is used with update, delete and insert statements.
An example of mysql_affected_rows().
$result = mysql_query( "INSERT INTO mysqltable VALUES( NULL, '$value' )" ); $rows = mysql_affected_rows(); // returns 1
An example of mysql_num_rows().
$result = mysql_query( "SELECT field FROM mysqltable" ); $rows = mysql_num_rows( $result );
Thanks for the very helpful info!
On a SELECT query, mysql_affected_rows() returns the number of rows found, while mysql_num_rows() returns the number of rows returned. Usually this is the same thing, but it could be different if you have LIMIT or GROUP BY or something similar that would alter the number of rows that get returned.
Thanks for the clarification. Thanks to a great long-tail keyword search I found this easy clarification which makes my late night of programming a little lighter. Thank you.