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 7797647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:45:05+00:00 2026-06-01T23:45:05+00:00

So I have a query, and though my query can possibly return 20 results,

  • 0

So I have a query, and though my query can possibly return 20 results, I only want it to show the first 9 results. There is a dumb reason I’m not just limiting the query results to 9, for this purpose I need to know how to stop the while function if $i reaches 9.

Code is

    $i = 0;
    while($array = mysql_fetch_array($queryresults) && $i <= '9')
    {
               echo $array['id'];
               $i++;
    }

How do I get it to stop putting out more echoes after 9th result? Thank you!

  • 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-06-01T23:45:06+00:00Added an answer on June 1, 2026 at 11:45 pm

    Putting the limiting condition first will also make sure you only fetch 9 rows, not and 10 and one discard because && is called lazy (if the first part is false, it wont even look at the next part).

    $i = 0;
    while($i < 9 && $array = mysql_fetch_array($queryresults)){
        echo $array['id'];
        $i += 1;
    }
    

    or if you want to be fancy:

    $i = 9;
    while($i-- > 0 && $array = mysql_fetch_array($queryresults)){
        echo $array['id'];
    }
    

    But I find this error prone ..

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

Sidebar

Related Questions

We have some search functionality that can return tens of thousands of results from
I want to fetch google images against any query. I have gone through the
I have a query that I want to execute that fastest possible. Here it
I'm generating a query and report through VBA. I have an option of asking
I have the following Access 2002 query I'm running through OLE DB in .NET:
I have made a form that accepts query and executes it through php. I
I have query with two MtM relations: $em = $this->getEntityManager(); $qb = $em->createQueryBuilder(); $qb
I have query table copyQuery which has Soil Condition has one of the columns,
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I am using in C# MYsql .I have query that works if I run

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.