Solved

How to know how many records came as a result

Posted on 2010-11-27
8
347 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
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 500 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

17 Experts available now in Live!

Get 1:1 Help Now