在mysql /php中使用while和if组合比较两个表的值


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 "] = $第一行[" 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 
        }
    }
}