簡體   English   中英

使用while和if組合比較mysql / php中兩個表的值

[英]Comparing values of two tables in mysql /php using while and if combination

是否可以使用兩個while語句比較兩個表中的值。 我正在使用以下方式不起作用。 請指出我的錯誤或建議我一些更容易使用的解決方案。

$sql = "select * from display where gp_no = '$gp_no' ";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0)
{

  $s = " select date,member_no  from bid where gp_no = '$gp_no ' "; 
  $r = mysqli_query($conn, $s);
  if (mysqli_num_rows($r) > 0)
   {
    while($row = mysqli_fetch_assoc($result))
     { 
      while($row1 = mysqli_fetch_assoc($r))
       { 
         if($row["member_no"] = $row1[ " member_no"])
          {
           //some code to display something 
        } // inner if
       } // inner while
      } // outer while
     } // outer if
    } // outermost if

您也可以使用其他方法。 如下所示:

$array1 = array('1','2','3');
$array2 = array('4','5','3');

foreach($array1 as $index=>$val)
{
    if($val == $array2[$index])
    {
    // go on
    }
}

您可以嘗試這樣的事情:

foreach($result as $index=>$val)
{
    if($val->member_no == $r[$index]->member_no)
    {
    // go on
    }
}

if($ row [“ member_no”] = $ row1 [“ member_no”])

==代替=


編輯:

比較使用while循環,對您來說更好嗎? 它管理不同大小的表:

if (mysqli_num_rows($r) > 0)
{
    $row_count = mysqli_num_rows($result);
    $row1_count = mysqli_num_rows($r);
    $remaining_rows = min($row_count, $row1_count);

    while($remaining_rows-- > 0)
    {
        $row = mysqli_fetch_assoc($result);
        $row1 = mysqli_fetch_assoc($r);
        if($row["member_no"] == $row1["member_no"])
        {
            //some code to display something 
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM