doubi3929 2015-12-13 19:34
浏览 77

如何在一个while循环中使用两个mysql_fetch_array()数组

Hi i have two arrays built by using mysql_fetch_array() i want to use them both in one while loop :

$username =$_SESSION["username"];
$password =$_SESSION["password"];       
$query1= "SELECT * FROM subs WHERE username='$username' AND password='$password' ";
$res1 = mysql_query($query1);
$row_cnt1 = mysql_num_rows($res1);
if($res1 === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}
$row1 = mysql_fetch_array($res1);
if($row_cnt1 == 1){
    $sid = $row1["id"];
}else{
    die("there is more than one user with the same username and password");
}


$query2= "SELECT * FROM bookreservations ";
$res2 = mysql_query($query2);
$row_cnt2 = mysql_num_rows($res2);
if($res2 === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}

$row2 = mysql_fetch_array($res2) ;

$query= "SELECT * FROM books";
$res = mysql_query($query);
$row_cnt = mysql_num_rows($res);
if($res === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}


    while ($row = mysql_fetch_array($res)){
        if($row2["sid"] == $sid && $row2["bid"] == $row["id"]){
            continue;
        }
        $temp = $row["id"];
        $temp1 = $row["title"];
        echo "Title : ".$temp1."<br><br>";
        $temp1 = $row["authors"];
        echo "Authors : ".$temp1."<br><br>";
        $temp1 = $row["copies"];
        echo "Copies : ".$temp1."<br><br><br><br>";

        echo "<div class=\"re\"><input type=\"radio\" value=\"reserve\" name=\"".$temp ."\">reserve</div> <br><br><br><br><br><br>" ;

    }

I want to extract things from row array but i don't want them to have some values from row2 array so I have to use them both in one while loop ,THE SUMMARY row array is ok it jumps to the next row in each loop but row2 array is stuck it doesn't move to the next row what to do ?

  • 写回答

1条回答 默认 最新

  • dongshan9338 2015-12-13 19:54
    关注

    Although I don't really understand your task well, I however, would recommend the following edits My assumption: You are trying to fetch all rows from $res for each $res2.

    Although inefficient technique, but I'll edit your code as below.

    while($row2 = mysql_fetch_array($res2) ){
    
    
    while ( $row = mysql_fetch_array($res) ){ //Both $row & $row2 will have equal incremented rows.
       }
    
    }//Outer loop
    
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示