Mysql – Difference in between MySQL affected num rows and num rows

MySQL

I am just newbie to the MySQL.I want to know what is the main differences in between mysql num rows and mysql affected num rows?Any example or references will be highly appreciable.

Best Answer

Here's mysql_num_rows:

int mysql_num_rows ( resource $result ) 

Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

And here's mysql_affected_rows:

int mysql_affected_rows ([ resource $link_identifier = NULL ] )

Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier.

Notice that the input types are different: the first wants a result set, from which it counts the number of rows returned; the second wants a db handle. That makes sense, though, because when you INSERT (or UPDATE, DELETE, REPLACE) a row, you typically don't want a result set -- you just want to know if it succeeded or failed, or how many rows it updated/replaced/deleted.