Search code examples
phpmysql-num-rows

mysql_num_rows and mysql_affected_rows return the same result when executing SELECT SQL


My test code:

    $connection = mysql_connect('localhost', 'root', '') or die(mysql_error());

    mysql_select_db("chaoge", $connection);

    mysql_query("SET NAMES UTF8", $connection);

    $rs = mysql_query("SELECT * FROM babel_node WHERE nod_pid = 2101", $connection);

    $nu = mysql_affected_rows();
    echo $nu;

It says that mysql_affected_rows works with INSERT, UPDATE, REPLACE and DELETE。

Why I can also get the right result through mysql_affected_rows ?

Any help and suggestions will be highly appreciable。


Solution

  • Here is what I found on the internet.

    mysql_affected_rows() for a SELECT indicates the number of rows which were found. mysql_num_rows() indicates how many rows were actually returned. They may not be the same, IIRC, if you have a LIMIT clause or similar. GROUP BY may also cause a difference.

    Source

    See answers on the source at the bottom.