2

Sometimes I would like to get the amount of results in a query, often I use mysqli_num_rows to do that, example:

$count = mysqli_query($this->db,"SELECT * FROM `test`");
return mysqli_num_rows($count);

With count:

$count = mysqli_query($this->db, "SELECT COUNT(*) as cnt FROM test");
$row = mysqli_fetch_assoc($count);
return $row['cnt'];

Is there any difference between these two ways to get the number of rows returned?

Piqka
  • 43
  • 7

1 Answers1

4

Yes.

The former one should never be used, because it will select all the table data from database and send it to PHP, choking server in the process.

While the latter one is selecting and sending a single number.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345