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

  • SEARCH
  • Home
  • 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 6025201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:16:01+00:00 2026-05-23T04:16:01+00:00

So I have a MySQL query that is being created dynamically based on parameters

  • 0

So I have a MySQL query that is being created dynamically based on parameters passed to my php script. It which is working great, however, I would like to be able to fetch the number of rows it would return before applying the LIMIT statement. It seems to me like this could not be done in one query, but there is so much about MySQL I don’t know that it would not surprise me in the slightest if you could.

I have looked around and found out that COUNT(*) is much faster than mysql_num_rows() but as far as I know, I can’t use either in my case since they would simply be limited by how many results I have specified in my LIMIT, which is usually 30. Also, I would rather not submit a query that doesn’t contain the LIMIT just to get the numbers or rows it would return and then immediately apply the LIMIT and submit the same query.

Here is a sample query:

SELECT DISTINCT w.id, w.resolution, w.views, w.colors, w.favorites  
FROM wallpapers AS w 
LEFT JOIN (tagmap AS tm, tags AS t) 
ON (
    w.id = tm.wallpaper_id 
    AND 
    tm.tag_id = t.tag_id
) WHERE w.ratio = 
    (SELECT ratio FROM wallpapers WHERE resolution = '1920 x 1080' LIMIT 1)
AND (
    t.tag
    IN (
        'portal'
    )
)
AND (w.verify = 2) 
ORDER BY w.id DESC LIMIT 30

I’ve cleaned that up and spent a lot of time optimising it so hopefully it is easy for you to read but also feel free to mention anything else you spot funky in the code that may help speed it up ^^

Back to the question, one way I see to do this would be to stick the conditionals (everything but the SELECT, JOIN, GROUP BY, AND LIMIT basically) from that query onto a SELECT COUNT(*) statement, run that, and then run the actual query to get all the data. Is that the most efficient way or does somebody have a better idea? I would love to hear all possible solutions, thanks! 🙂

  • 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-23T04:16:01+00:00Added an answer on May 23, 2026 at 4:16 am

    I think what you are looking for is CALC_FOUND_ROWS

    SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
    WHERE id > 100 LIMIT 10;
    
    SELECT FOUND_ROWS();
    

    It’s still two queries though.

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

Sidebar

Related Questions

I have a drop down list that is dynamically generated using a mySQL database
I just started with PHP and my SQL databases, I have learned how to
I have been able to create php function to determine a list of zip
I'm building a small scale forum script in PHP, and I am having difficulty
I have a website that grabs a random entry from a database and displays
I have an array with the result of a query: .....some code..... $result =
This is my first post and I have the feeling that this is something
I have a system where reservations are stored as records in one table(workshop_reservation) and
I have a symfony website running on the standard LAMP setup. I need to
I have a website built across a series of pages and scripts, and I'm

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.