How to know how many records came as a result

Posted on 2010-11-27
Medium Priority
Last Modified: 2012-05-10
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?

Question by:doRodrigo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 15

Expert Comment

ID: 34221769
After running the query use mysql_num_rows.


$num_rows = mysql_num_rows($result);
LVL 37

Expert Comment

by:Neil Russell
ID: 34221774
You use
int mysql_num_rows ( resource $result )
LVL 15

Accepted Solution

ncoo earned 2000 total points
ID: 34221778
The other option would be to use mysql count by changing your query to the following.

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

if ($row = mysql_fetch_assoc($result)) {
 echo $row['total'].' rows';

Open in new window

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more


Author Closing Comment

ID: 34221912
Cheers mate that was what I needed!
LVL 15

Expert Comment

ID: 34225905

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:


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


Author Comment

ID: 34225928
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.


Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question