2

Possible Duplicate:
How can I count the numbers of rows that a mysql query returned?

To get the length of the return of a mysql result, I am using:

$counter=0;
while($row = mysql_fetch_array($mysqlqueryresult)){
$counter++;
}
echo $counter;

Is there a better way to get the number of rows returned by mysql?

Note - Not allowed to use COUNT in the mysql or any mysql tricks, only php on the mysql output as here. So starting point is a mysql returned thing of $mysqlqueryresult; which itself is the result of a basic SELECT query.

Community
  • 1
  • 1
David19801
  • 11,214
  • 25
  • 84
  • 127

5 Answers5

7

Use mysql_num_rows.

$counter = mysql_num_rows($mysqlqueryresult);

If you've updated the table, the number of affected rows is given by mysql_affected_rows.

Rob W
  • 341,306
  • 83
  • 791
  • 678
2

Use mysql_num_rows($mysqlqueryresult)

Nikoloff
  • 4,050
  • 1
  • 17
  • 19
0

mysql_num_rows() is the function you're looking for.

edit. I arrived too late. ;)

Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98
0

you Can use mysql_num_rows() routine .

$counter = mysql_num_rows($mysqlqueryresult);
Vijeenrosh P.W
  • 359
  • 1
  • 3
  • 8
0

Well, just for the method's sake (although mysql_num_rows() is the preferred method), a pure php one without using a loop:

count($mysql_fetch_array($mysqlqueryresult));

(or am I completely mistaken?)

alex
  • 1,277
  • 3
  • 14
  • 30