dongwopu8210 2014-12-05 06:06
浏览 38
已采纳

php嵌套while循环。 内环只能工作一次[关闭]

This is the code . Any help ??

<?php  

    //connect to the database 
        $con=mysql_connect("localhost","root","") or die("Error in connection");
        mysql_select_db("riffadb",$con)or die("Error in database");
    // 
        $sql1 ="select category_id,category_code from category";
        $res1 = mysql_query($sql1); 

        $sql2 = "select category_id1,product_id  from product";
        $res2 = mysql_query($sql2);
        $i=0;
        $j=1000;
        while($row1 = mysql_fetch_array($res1))
        {
            echo $row1['category_code'];
            echo '<br/>';
            while($row2 = mysql_fetch_array($res2))
            {   
                if($row1['category_code'] == $row2['category_id1'])
                {   
                    $sql3 = "update product set category_id = ".$row1['category_id']." where product_id = ".$row2['product_id'];
                    echo '<br/>';
                    mysql_query($sql3);
                }
            }
            $i++;
        }
        echo $i;
    ?>
  • 写回答

3条回答 默认 最新

  • doumen1883 2014-12-05 06:20
    关注

    I think you need to re-query res2 inside the outer loop, or at least reset the pointer. The second time through the outer loop you have nothing left in res2, since you've already reached the end

    In addition, with would be a lot more efficient if you setup the res2 query inside the outer loop and add a where clause so that you are only matching the category ID from the current row in res1.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集