Feb
4

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

  • Leila
    11:27 am on February 11th, 2010:
    1

    Thanks for the very helpful info!

  • Brad Proctor
    6:06 pm on September 22nd, 2011:
    2

    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.

  • Web Design Inverness
    12:31 am on December 7th, 2011:
    3

    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

Mail will not be published.

© 2012 · Karl Sheen Blog · Sitemap · Proudly powered by Wordpress and Twitter Bootstrap · Icons by Glyphicons