douzhi9921 2015-12-08 22:47
浏览 61
已采纳

while循环里面的foreach循环里面循环[关闭]

I know just the title hurt. I feel like i'm in inception. I am using 2 different databases. I am trying to use a while loop to pick up 4 values from the first database using a while loop. Then I want to use a while loop inside a foreach loop to find the name and the related law for each of those 4 values. I have tried different codes this one gets me the closest but only returns the first value not the other three.

$link1 = mysql_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_dblogin);
if(!$link1 || !mysql_select_db ($mysql_dblogin)) {
die ($conn_error_message);
}

$link2 = mysql_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_db, TRUE);
if(!$link2 || !mysql_select_db ($mysql_db)) {
die ($conn_error_message);
}



$sql="SELECT loi FROM $username";
$result = mysql_query("$sql", $link1);
$data = array();
if($result){
WHILE($row=mysql_fetch_assoc($result)){
$data[]=$row;
foreach($data as $vote){
        $loi = $vote['loi'];
        $sql="SELECT objet, colo FROM scruinfo WHERE colo = '$loi' ";
        $result=mysql_query("$sql", $link2);
        if($result){
            WHILE($row=mysql_fetch_assoc($result)){
            $loi = $row["colo"];
            $name = $row["objet"];
            echo "<table><td href='votes.php?loi=".$loi."'>".$name."</td></table><br>";
            }
        }
    }
}
}else{
echo mysql_error();
}
?>
  • 写回答

2条回答 默认 最新

  • doujia1988 2015-12-08 23:01
    关注

    Inside foreach you are updating $result and $row with new values, so the outer while will stop to loop.
    Rename those variables and it'll work!

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

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'