Solved

How to know how many records came as a result

Posted on 2010-11-27
8
346 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
  • 3
  • 2
8 Comments
 
LVL 15

Expert Comment

by:ncoo
Comment Utility
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
Comment Utility
You use
int mysql_num_rows ( resource $result )
0
 
LVL 15

Accepted Solution

by:
ncoo earned 500 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:doRodrigo
Comment Utility
Cheers mate that was what I needed!
0
 
LVL 15

Expert Comment

by:ncoo
Comment Utility
@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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Creating and Managing Databases with phpMyAdmin in cPanel.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now