Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:38:01+00:00 2026-05-26T23:38:01+00:00

I want to count all the rows and at the same time list them

  • 0

I want to count all the rows and at the same time list them as I usually do, using mysql_fetch_object. I thought I’d do something like this;

$total = mysql_query("SELECT *, COUNT(*) AS totalfound FROM img WHERE state='0'");

But I can’t seem to wrap my head around how to get the values out – I just get the first item in the table when I run this;

while ($record = mysql_fetch_object($total)) { echo $record->id; }

If I want to get the totalfound I could do this;

$result = mysql_fetch_assoc($total);
$count = $result['totalfound'];

…but then I can’t get the rest. I know I’m thinking wrong but can’t seem to get it to work. Can you guys please help me out? Thanks!

Once thing I forgot to mention: mysql_num_rows is too slow, I was thinking of using count(*) instead. As an example, mysql_num_rows on the entire table takes everything from 3 to 9 seconds, and a count(*) always takes 0.6 seconds, getting the same results.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:38:02+00:00Added an answer on May 26, 2026 at 11:38 pm

    A count is an aggregate function, you cannot select both rows and aggregates without using group by. MySQL will let you do it without the group by, and will produce unpredictable results.

    Just use the query without count to get the rows, and use mysql_num_rows() on the result.

    Edit: If mysql_num_rows() is slow, you must be returning a lot of rows. You’d then better execute two queries, one simply select count(*) as numrows ..., and one to retreive your data.

    Try to add proper indexes and the count(*) will execute within a few miliseconds.

    You really don’t want this in one query. Every row will then have a column that states how many rows there are, which is unrelated to that row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find all the rows with the same value in. (Or at
I have a table with rows like id, length, time and some of them
We've got a PHP application and want to count all the lines of code
Suppose I want to count the lines of code in a project. If all
This is the code that I'm currently using. But I want it to take
Strange question, I know. I don't want to delete all the rows and start
I am trying to find duplicate rows in my DB, like this: SELECT email,
I have a table: ID name c_counts f_counts and I want to order all
I want to count the number of set bits in a uint in Specman:
I want to count how many times i built on VS.NET . I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.