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 );

3 Comments on “mysql_num_rows() VS mysql_affected_rows()”

  1. 1 Leila said at 11:27 am on February 11th, 2010:

    Thanks for the very helpful info!

  2. 2 Brad Proctor said at 6:06 pm on September 22nd, 2011:

    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.

  3. 3 Web Design Inverness said at 12:31 am on December 7th, 2011:

    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.

Leave a Reply

  • Name (required)
  • Mail (will not be published) (required)
  • Website
  • Message