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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:57:50+00:00 2026-05-29T08:57:50+00:00

i am trying to fetch result of a query that is in a loop

  • 0

i am trying to fetch result of a query that is in a loop but i cant get all the records returned by query that is in the loop please guide me.

  $qry = '';
    $qry = "SELECT    
            `user_id`  FROM 
            `view_property_user_lab`";
    $qry .= " WHERE property_id = '" . $propId . "'";
    $result = $this->db->query($qry);
    $result_count = $result->result_array();
    foreach ($result_count as $row)
    {
        $user_name_qry = "SELECT
                                 `name`
                                    FROM USER
                                    WHERE user_id =  '" . $row['user_id'] . "'";
                                   $user_name_result = $this->db->query($user_name_qry);
    }

    $result_count_user = $user_name_result->result_array();
    print_r($result_count_user);

This is the dump of array

 Array ( [0] => Array ( [name] => abc) )
  • 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-29T08:57:51+00:00Added an answer on May 29, 2026 at 8:57 am

    I think your problem is that you’re using a foreach loop, but everytime you overwrite the same variable ($user_name_result) and you use only the last one assigned to get the data from the result resource (since you’re calling that outside the loop!).

    But all in all, as tersko said in the comments, doing queries in loops isn’t a good thing (hitting your db the more your users increase).

    You can simplify everything with a JOINed query (just guessing your table schema here, though):

    $sql = "SELECT u.name FROM view_property_user_lab AS v JOIN user AS u ON u.user_id = v.user_id WHERE v.property_id = ?";
    $query = $this->db->query($sql,array($propID));
    
    print_r($query->result_array());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to query a table, fetch all records, and save the result as
I am trying to write a query that would fetch the number of player
I'm trying to fetch data from a page that resides on an intranet. The
I am trying to fetch all jobs registered with the Quartz scheduler for a
I'm trying to dynamically bind mysql_stmt parameters and get the result in an associative
I have a question about PHP Class. I am trying to get the result
I'm trying to write my first query that uses a database join. I've gone
I'm trying to get my postgresql to work. I've worked with sql before, but
Im trying to create a function, that will return a mysql query, which i
I'm trying to write a simple Ubiquity command that will run a query on

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.