View Single Post
  #5 (permalink)  
Old November 10th, 2003, 02:53 PM
nikolai nikolai is offline
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

Yeah, I don't see any reason why you don't just get all the information in a single query. In fact, I can't think of any good way of using TWO queries and loops to do it. Since MySQL is a relational database, it deals with sets of data, with no specific order imposed on the result set unless you specify one. That said, there's really no guarantee that the data you're trying to output in the second loop is the data corresponding to the row being used in the first loop.

Actually, I take that back -- there is a way you can use two loops, but you'll make several queries:

$result1 = mysql_query($query1);
while($row1 = mysql_fetch_array($result))
    $query2 = "SELECT ... WHERE the data corresponds to something in $row1";
    $result = mysql_query($query2);
    while($row2 = mysql_fetch_array($result2))

But that's obviously pretty inefficient, since you're making an additional database query for each iteration of the loop.

Since the first query is only there to give you your category names, why not return the category name as part of the second query?

Take care,

Reply With Quote