Solved

select count(*) with mysql is one too large

Posted on 2004-08-07
4
18,547 Views
Last Modified: 2013-12-12
Hello-
    When I run the following:

      $recordCount = "select count(*) from memberInfo where lname like 'B%'";
      $totalRowsResult = mysql_query($recordCount,$conn);
      $totalRows = mysql_fetch_row($totalRowsResult);
      echo $totalRows[0];

$totalRows[0] = 25. However, if I run select * from memberInfo where lname like 'B%' it returns 24 records. Why is select count(*) counting one too many when run within PHP? If i run both queries from the MySQL client, they both indicate that there are 24 rows. Do I just always have to subtract one from whatever is returned when I do select count(*) from PHP?

Thanks-
Joel
0
Comment
Question by:jdaynes
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 50 total points
ID: 11743992
>> If i run both queries from the MySQL client, they both indicate that there are 24 rows

Then you *must* get 24 rows unless you have some more additional codes/changes in your PHP.
0
 

Author Comment

by:jdaynes
ID: 11744015
wow--it looks like I'm an idiot. I accidentally included a % in one like clause that wasn't there in another, causing the discrepancy. Sorry for my stupidity....
0
 
LVL 4

Expert Comment

by:VIkasumit
ID: 11910303
Hi..

I know that the Answer is already accepted But I want to clear you that the two queries you are talking about are completely different ..

count(*) return that you have 25 records in the database matching the where clause....

and select * from ... return the first records first column not the COUNT as you refer....
If you want to see how many records are there in the second query you should use mysql_num_rows( $totalRowsResult ) ...

I hope you read this and Correct your logic there...

VIkasumit
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11911069
>> I hope you read this and Correct your logic there...

I dont think there is something wrong in the logic.

The author justed wanted to *verify* the number of records with SELECT * FROM memberInfo WHERE lname LIKE 'B%' and SELECT count(*) FROM memberInfo WHERE lname LIKE 'B%'.

Ofcourse, both are different but the former will return 24 rows and the later returns 24.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Permalink issue in Wordpress archive 1 32
Please show me easiest post form. 3 31
Amazon Redshift 2 24
How to loop bootstrap columns which contain database records 9 18
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

929 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

15 Experts available now in Live!

Get 1:1 Help Now