简体   繁体   中英

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

Is it possible to compare the values from two tables using two while statements. I was using the following way which didn't work . Please either point out my mistake or suggest me something easier working solution .

$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

You can also use a different approach. Something like below :

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

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

You can try something like this :

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

if($row["member_no"] = $row1[ " member_no"])

== instead of = ?


EDIT:

Compares using one while loop, is it better for you? It manage different sized tables:

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 
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM