我有兩個表,一個是類別,另一個是products.Catid對於這兩個表都是很常見的。我需要使用兩個while循環來顯示錶中的數據。它的工作正常,但花費更多的時間,並拋出錯誤,如果它處理更多的類別。如何在php中優化mysql嵌套循環查詢
<table>
<tr>
<?php
$cat=mysql_query("select * from category");
while($catquery=mysql_fetch_assoc($cat))
{?>
<td><?php echo $catquery['catid'] ?></td>
<td><?php echo $catquery['catname']?></td>
<?php
$catid=$catquery['catid'];
}?>
<?php
$product=mysql_query("select * from product where catid=$catid");
while($productquery=mysql_fetch_assoc($product))
{
?>
<td><?php echo $productquery['productname'] ?></td>
</tr>
</table>
你不能在查詢中使用聯接? – 2015-02-07 06:51:36
如果我使用了內部連接,則爲每個產品名稱創建單獨的行。但我需要顯示所有產品的名稱在相應類別ID的單行中。 – 2015-02-07 06:53:13
需要看到db結構 – 2015-02-07 06:57:34