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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:21:25+00:00 2026-05-31T04:21:25+00:00

i allways end up doing this $q = select whatIwant FROM table where id

  • 0

i allways end up doing this

$q = "select whatIwant FROM table where id = 'myId'";
$r = mysql_query($q);
while($i = mysql_fetch_array($r){
   /* iterate just one withem */
   $j = $i['whatIwant'];
}
echo $j;

how is this usually done? (i just want to avoid the unecessary loop)

  • 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-31T04:21:26+00:00Added an answer on May 31, 2026 at 4:21 am

    In addition to the correct answers, there are multiple ways to handle this:

    • If you add LIMIT 1, the result set will only contain one row and the while loop will terminate after one iteration:

      $q = "select whatIwant FROM table where id = 'myId' LIMIT 1";
      $r = mysql_query($q);
      while($i = mysql_fetch_array($r)) {
         $j = $i['whatIwant'];
      }
      echo $j;
      
    • If you call mysql_fetch_array without a loop, you will get the first row of the result set:

      $q = "select whatIwant FROM table where id = 'myId'";
      $r = mysql_query($q);
      $i = mysql_fetch_array($r);
      $j = $i['whatIwant'];
      echo $j;
      
    • If you add break to the loop body, the loop will terminate after one iteration.

      $q = "select whatIwant FROM table where id = 'myId'";
      $r = mysql_query($q);
      while($i = mysql_fetch_array($r)) {
         $j = $i['whatIwant'];
         break;
      }
      echo $j;
      

    You can also combine these approaches (although using break is not really elegant in this case).

    The best approach is using LIMIT and omitting the while loop, as @zaf shows in his answer. This makes the code clearer and avoids unnecessary operations in the database.

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

Sidebar

Related Questions

This is a problem I come across every so often; I always end up
I am trying to deserialize a stream but I always get this error End
Im trying to create a field called Owner in my table where You select
At the moment, I select rows from 'table01 and table02' using: SELECT t1.*,t2.* FROM
We are trying to optimize some of our queries. One query is doing the
I am trying to do a mysql query to select some news stories from
I have an SP with some condition like this IF NOT EXISTS (Select *
Ok, I have two tables that I am joining and doing a select on
(A while back I read this great post: http://aaronlongwell.com/2009/06/the-ruby-on-rails-cms-dilemma.html , discussing the Rails CMS
I always endup doing this to cover all situations where @variable can be empty

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.