0

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
naggarwal11
  • 122
  • 1
  • 14

2 Answers2

2

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
    }
}
Happy Coding
  • 2,517
  • 1
  • 13
  • 24
  • emmm Yea I was also thinking for this one , but then was stuck because then I would need a dynamic array so that I can take the values from one table and fill it into an array . So, Could you tell me what I'm supposed to do to get that ? – naggarwal11 Sep 22 '15 at 11:49
  • I'm sorry but i'm a bit newbie to all this . Can you please tell me what is doing what ? I understood `$result` is the same as in my Question . So , I know what to feed it with. What is `$index`, `$val` . I assume `$r` is also doing the same job as in my Question it was doing ? Please elaborate . – naggarwal11 Sep 22 '15 at 12:16
  • Read this tutorials..http://www.w3schools.com/php/php_looping_for.asp i think you are not much familiar with foreach loop – Happy Coding Sep 22 '15 at 12:20
1

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 
        }
    }
}
Thomas LAURENT
  • 1,592
  • 1
  • 11
  • 9
  • First of all tell me , is it possible to have multiple fetches like I did ? – naggarwal11 Sep 22 '15 at 11:47
  • I'm getting an error `Notice: Undefined index: member_no in /home/sdrtracks/bla/bla ` pointing at this line , the line you've written above . – naggarwal11 Sep 22 '15 at 11:57
  • 1
    @naggarwal11 remove space in `$row1["member_no"])` – akasummer Sep 22 '15 at 12:02
  • Well unexpectedly it removed the error . Upvote for that. But still it didn't solve my problem. Is it possible to run two loops in parallel . I mean neither as a nested one nor in a sequence of one two . I want both of them to be executed at the same time . The two while loops I've written above in the Question ? @akasummer – naggarwal11 Sep 22 '15 at 12:09
  • @naggarwal11 Since mysqli_fetch_assoc() accepts the mysqli_query return value as argument I think so. – Thomas LAURENT Sep 22 '15 at 12:10
  • @ThomasLAURENT : Thanks buddy !! Its working . A small problem is that it isn't recognizing the matched rows (i.e., `if (row["member_no"] == row1["member_no"]) ` ) . it is displaying the results of `else` part . Is there we need to change the compare operator ? – naggarwal11 Sep 24 '15 at 06:11
  • Maybe try to replace == to === (see http://stackoverflow.com/a/8494892/4052438 and the two comments below) – Thomas LAURENT Sep 24 '15 at 08:20