Page 1 of 1

Return values based on results of another table.

Posted: Tue Mar 09, 2010 9:02 am
by Vestax159
So I'm trying to return values from one table based on the results of another. The problem is that the way I have it now it won't allow me to use ORDER BY because it's working with one value at a time. Could anyone propose an alternate way to do this?

Code: Select all

 
$getcategories = tep_db_query("SELECT * FROM products_to_categories WHERE categories_id ='" . $category . "'")
or die ("Couldn’t execute query.");
    
 
while ($row = tep_db_fetch_array($getcategories))
{
    extract($row);
    
    $getproducts = tep_db_query("SELECT products_name products_price FROM products WHERE products_id ='" .  $products_id . "'");
 
    
    while ($rname = tep_db_fetch_array($getproducts))
        {
        extract ($rname);
        
            echo "print results";
        }
 
}
 

Re: Return values based on results of another table.

Posted: Tue Mar 09, 2010 9:09 am
by N1gel
You could do it all in one Query using a Join. Then add your order by clause

I presume you are using something like MySQL. Try having a look in the MySQL Manual

http://dev.mysql.com/doc/refman/5.1/en/left-join-optimization.html

Re: Return values based on results of another table.

Posted: Tue Mar 09, 2010 11:11 am
by Vestax159
Thanks! Worked wonderfully