سؤال

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);
    }
هل كانت مفيدة؟

المحلول

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top