?
Solved

How to know how many records came as a result

Posted on 2010-11-27
8
Medium Priority
?
356 Views
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?

Cheers!
0
Comment
Question by:doRodrigo
[X]
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
8 Comments
 
LVL 15

Expert Comment

by:ncoo
ID: 34221769
After running the query use mysql_num_rows.

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

$num_rows = mysql_num_rows($result);
0
 
LVL 37

Expert Comment

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

Accepted Solution

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

http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
$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

0
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

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

Expert Comment

by:ncoo
ID: 34225905
@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

0
 

Author Comment

by:doRodrigo
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.

Cheers!
0

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