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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog