I have a table which contains a list of categories and and another table which contains a list of products in each category.
e.g.
CatID | Cat_Name
----------------
1 | Books
2 | CDs
and
ProductID | Product_Name | CatID
------------------------------------
1 |The Bible | 1
2 |The Koran | 1
3 |90s Greatest Hits | 2
4 |80s Greatest Hits | 2
what I want to do is get
<ul>
<li>Books</li>
<ul>
<li>The Bible</li>
<li>The Koran</li>
</ul>
<li>Cds</li>
<ul>
<li>90s Greatest Hits </li>
<li>00s Greatest Hits </li>
</ul>
</ul>
without doing (PHP)
$query = mysql_query("SELECT ... FROM categories")
while($row = mysql_fetch_assoc($query)):
$query2 = mysql_query("SELECT ... FROM products WHERE catId = $row['CatId'])
endwhile;
How can I move away from using nested while/SELECT statements, can I do it efficiently with one query - and how do I access the relevant data using PHP?
EDIT: I was under the impression that if your tables become quite large, multiple SQL queries will slow your page, and therefore to optimize page load you should try to reduce the number of queries?