MYSQL: yerine while döngüsü, SELECT ifadeleri kullanarak - benim seçenekleri nelerdir?

0 Cevap php

Ben her kategoride ürünlerin bir listesini içerir kategoriler ve başka tablodaki bir listesini içeren bir tablo var.

örneğin

CatID | Cat_Name
----------------
1     | Books
2     | CDs

ve

ProductID | Product_Name     | CatID
------------------------------------
1         |The Bible         | 1
2         |The Koran         | 1
3         |90s Greatest Hits | 2
4         |80s Greatest Hits | 2

ne yapmak istiyorum olsun

<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>

yapmadan (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 - ve 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, ve therefore to optimize page load you should try to reduce the number of queries?

0 Cevap