Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to know how many records came as a result

Posted on 2010-11-27
8
Medium Priority
?
360 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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…
Suggested Courses

618 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