There are a few differences between the two:
num_rows
is the number of result rows (records) received.
count(*)
is the number of records in the database matching the query.
The database may be configured to limit the number of returned results (MySQL allows this for instance), in which case the two may differ in value if the limit is lower than the number of matching records. Note that limits may be configured by the DBA, so it may not be obvious from the SQL query code itself what limits apply.
Using num_rows
to count records implies "transmitting" each record, so if you only want a total number (which would be a single record/row) you are far better off getting the count
instead.
Additionally count
can be used in more complex query scenario's to do things like sub-totals, which is not easily done with num_rows
.
num_rows()
in PHP to count them, then you need to pass a million rows from the database to PHP. If you're usingcount(*)
on the database, then you're passing one row to PHP.COUNT(*)
will also have to count the rows, only on the DB server.count(*)
as that involves transferring less data. But your database software is designed to optimize your queries to run efficiently, so why not take advantage of that? As others have said, though, for a small amount of data, you're probably not going to see much actual difference.