Link to home
Start Free TrialLog in
Avatar of doRodrigo
doRodrigoFlag for New Zealand

asked on

How to know how many records came as a result

Hi folks I`m trying to get the number of records from a query on a MySql database but I`m having some trouble. The query itself is the following?

$result = mysql_query("SELECT * FROM tbLinks ORDER BY id DESC", $connection);

How do I get the number of records that came as a result?

Cheers!
Avatar of ncoo
ncoo

After running the query use mysql_num_rows.

http://php.net/manual/en/function.mysql-num-rows.php

$num_rows = mysql_num_rows($result);
You use
int mysql_num_rows ( resource $result )
ASKER CERTIFIED SOLUTION
Avatar of ncoo
ncoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doRodrigo

ASKER

Cheers mate that was what I needed!
@aikimark

Just my point on this, if all that is required is a count of the records then using mysql count() is much quicker. See following discussions on the topic:

http://www.phpbuilder.com/board/archive/index.php/t-10233374.html
http://forums.mysql.com/read.php?115,55854,55854

However I will accept your point on the ORDER BY in the clause, that was perhaps sloppy on my part and is not needed with the count().

The query would be much better written as follows:
SELECT count(id) AS total FROM tbLinks

Open in new window

aikimark, the reason I decided to award the points to ncoo is that he had one of the first answers and even got to the point to offer a different solution, that even if it was not as elegant it was easy fr me to understand and classify it as the winner.

Cheers!