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
2
  • why don't you try subquery or join query?? Commented Sep 22, 2015 at 11:47
  • because I've already created a view using join . That is my one table(view) . Now , it isn't possible to join that view with another table because it will give false results then . Commented Sep 22, 2015 at 11:51

2 Answers 2

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
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
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 .
Read this tutorials..w3schools.com/php/php_looping_for.asp i think you are not much familiar with foreach loop
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 
        }
    }
}

7 Comments

First of all tell me , is it possible to have multiple fetches like I did ?
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 remove space in $row1["member_no"])
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 Since mysqli_fetch_assoc() accepts the mysqli_query return value as argument I think so.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.