Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何优化php中嵌套循环的mysql查询_Php_Mysql_While Loop - Fatal编程技术网

如何优化php中嵌套循环的mysql查询

如何优化php中嵌套循环的mysql查询,php,mysql,while-loop,Php,Mysql,While Loop,我有两个表,一个是category,另一个是products。两个表都有Catid。我需要使用两个while循环来显示表中的数据。它的工作很好,但如果它处理更多的类别,则需要更多的时间和抛出错误 <table> <tr> <?php $cat=mysql_query("select * from category"); while($catquery=mysql_fetch_assoc($cat)) {?> <td><?php echo

我有两个表,一个是category,另一个是products。两个表都有Catid。我需要使用两个while循环来显示表中的数据。它的工作很好,但如果它处理更多的类别,则需要更多的时间和抛出错误

   <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>
更好地逐个定义列,便于将数据导入PHP,如:

select a.catid as category_catid, 
       a.catname as category_name,
       b.productname as product_productname
from category a left join product b
on a.catid = b.catid
如果要显示所有产品,即使catid已在类别中删除,也可以将其与完全连接一起使用:

以下是获取数据的方式:

while($catquery=mysql_fetch_assoc($cat))
{?>
<td><?php echo $catquery['category_catid'] ?></td>
<td><?php echo $catquery['category_name']?></td>
<td><?php echo $catquery['product_productname']?></td>
....

尝试此查询。它将在下面的查询中列出所有类别详细信息及其各自的产品名称CATProducts

<table>

<?php
$cat=mysql_query("select *,(select group_concat(productname) as catproducts from product where product.catid= category.catid) as catproducts from category");
while($catquery=mysql_fetch_assoc($cat)) { ?>
  <tr>
  <td><?php echo $catquery['catid']; ?></td>
  <td><?php echo $catquery['catname'];?></td>
  <td><?php echo $catquery['catproducts']; ?></td>
  </tr>
<?php }//end while ?>

</table>

您不能在查询中使用联接吗?如果我使用了内部联接,则会为每个产品名称创建单独的行。但是我需要将所有产品的名称显示在各自类别id的一行中。需要查看db结构您能用表格和while循环简要更新代码吗@PritzzcatProducts显示未定义的索引错误。它不起作用@pritzzcheck再次检查此行-$catquery['catproducts']-和-group\U concatproductname作为catproducts此行确保拼写相同。我已验证。还是同一个错误。你有输出吗?@普里茨:是的。你能添加你的代码吗。编辑您的实际问题。每个产品名称显示在类别的单独行中@颜色Dalnet看起来您想打印一次类别?是的。。通过我的代码,我可以得到输出,但由于嵌套的while循环,需要花费大量的时间来处理大量数据。@在这里,创建$temp变量,并将其与catid进行比较。如果相同,则不要回声。但要确保它是按catid排序的。
while($catquery=mysql_fetch_assoc($cat))
{?>
<td><?php echo $catquery['category_catid'] ?></td>
<td><?php echo $catquery['category_name']?></td>
<td><?php echo $catquery['product_productname']?></td>
....
<table>

<?php
$cat=mysql_query("select *,(select group_concat(productname) as catproducts from product where product.catid= category.catid) as catproducts from category");
while($catquery=mysql_fetch_assoc($cat)) { ?>
  <tr>
  <td><?php echo $catquery['catid']; ?></td>
  <td><?php echo $catquery['catname'];?></td>
  <td><?php echo $catquery['catproducts']; ?></td>
  </tr>
<?php }//end while ?>

</table>
catid  catname      catproducts

1      stationary   book,pen
2      leather      wallets,bags