How many rows in mysql before fetch_assoc?

I get some data from mysql via select.

$result = mysqli_query($cxn, $query); 

Then I look through it with

 if ($result!= false) { while ($row = mysqli_fetch_assoc($result)) { do_stuff(); } } 

What I would like to do is add a line before while ONLY if the result is more than one line.

I understand that at first I could create an array from $row and then count them, but this may interfere with the convenience of while .

Question: is there a way to find out how many rows mysqli_fetch_assoc will generate without running it twice?

+4
source share
2 answers

You should check this function: http://php.net/mysqli_num_rows

You can check the number of results using this as

 if (mysqli_num_rows($result)) { .. } 
+5
source

use http://php.net/mysqli_num_rows , which returns the number of rows as a result

 if(mysqli_num_rows($result) > 1) 
+2
source

All Articles