我有两个表,一个是类别,另一个是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