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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:39:48+00:00 2026-05-31T21:39:48+00:00

For example I have a mysql query that gets some data. Then runs another

  • 0

For example I have a mysql query that gets some data. Then runs another query based some of the data that it got.

If i just return the first query, in my case $qOne. Everything works great.

BUT, after using my while loop while ($row = mysql_fetch_array($qOne)) It then returns as an empty array. (but the second query I return DOES work)

I tried to see if I could “save” the first query in another var im not messing with like this $savedResult = $qOne, then i’d just return the $savedResult but that did not work.

Does anyone know how I can get my function below to return both of the results? Thanks!

function getFoods($sort, $start, $limit) {

        $qOne = mysql_query("SELECT a.id, a.name, a.type, AVG(b.r) AS fra, COUNT(b.id) as tvotes FROM `foods` a LEFT JOIN `foods_ratings` b ON a.id = b.id GROUP BY a.id ORDER BY fra DESC, tvotes DESC LIMIT $start, $limit;");

        $i = 0;
        $qry = "";
        while ($row = mysql_fetch_array($qOne)) {
            $fid = $row['id'];
            if ($i > 0)
                $qry .= " UNION ";
            $i++;
            $qry .= "SELECT fid, ing, amount FROM foods_ing WHERE fid='$fid'";
        }

        $qTwo = mysql_query($qry);

        return array($qOne, $qTwo);
    }
  • 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-31T21:39:49+00:00Added an answer on May 31, 2026 at 9:39 pm

    When you have returned the two query result resources, remember that you will need to fetch from them when you actually want to use them. (we don’t see the code where you implement that).

    To make use of $qOne after you already have looped through it, you must rewind it back to the first position. That is done with mysql_data_seek()

    mysql_data_seek($qOne, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a mySQL query that produces an empty set. For example, SELECT id
I have got a MySql query that orders me results by no column (int,
I have a mysql query that shows the users last viewed documents. Some sort
I have a MySQL query that results in something like this: person | some_info
I have a MySQL query that uses two different timestamps that can be perceived
I have a very complex MySQL query that includes use of the same subquery
I have MySQL query that selects user names and theirs knowledge (may be more
For example I have a mysql_num_rows results of 4,8,15,16,23,42 in a query that is
I have a query that transfers data from multiple slave tables into a single
Anyone knows how to get sum of number? For example i have mysql column

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.