Ahmad Azizov
Ahmad Azizov

Reputation: 166

nested while loops not working?

i am having a "nonsense" problem with php. The first loop is supposed to get a certain record from the table and compare it to all the record in the second table...

So i expected it to print 41 "2nd"'s after every "1st"'s. Since there are 41 records in the second table. But instead the while loop works the first time and ignores the second while loop afterwards.

The result i get:

1st2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd2nd1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st1st...

while($row = mysql_fetch_array($select))
{
    echo "1st";
    while($row2 = mysql_fetch_array($select2))
    {
        echo "2nd";
        $string = $row2["header"];
        $find = $row["email"];
        if(strstr($string, $find)) {
            $email = $row["email"];
            echo "found it";
        } else {
            //no email found
        }
    }
}

Upvotes: 1

Views: 3704

Answers (4)

Antony
Antony

Reputation: 15104

Do not run mysql_fetch_array inside another while loop. Extract all the data first and compare the results from the two tables.

$select=mysql_query("SELECT email FROM database.tablename");
$select2=mysql_query("SELECT header FROM database2.tablename2");    
while($row=mysql_fetch_array($select)) {
    $first[] = $row["email"];
}
while($row2=mysql_fetch_array($select2)) {
    $second[] = $row2["header"];
}
foreach ($first as $item) {
    if (($key = array_search($item, $second)) !== false) {
        $email[] = $second[$key];
    }
}
print_r($email); // Get all the emails that exist in both tables.

Upvotes: 0

William N
William N

Reputation: 430

Unless its the same amount of rows in both queries, it will ofcourse not post the same.
Could you add queries ($select and $select2)?

And could someone tell me how to post comments instead of answers?

<?php
    $select=mysql_query("SELECT email FROM database.tablename"); 
    $select2=mysql_query("SELECT header FROM database2.tablename2");

    while($row=mysql_fetch_assoc($select))
    {
        echo "1st";
        $find = $row["email"];
        while($row2=mysql_fetch_assoc($select2))
        {
            echo "2nd";
            $string = $row2["header"];
            if(strstr($string, $find))
            {
                $email=$find;
                echo "found it";
            }
             else
            {
                  //no email found
            }

        }
    }

Upvotes: 0

Andrey Svirsky
Andrey Svirsky

Reputation: 116

You need to execute second query before entering inner loop:


    while($row=mysql_fetch_array($select))
    {
        echo "1st";

        $select2 = mysql_query("select ...");

        while($row2=mysql_fetch_array($select2))
        {

Better yet is to run second query before starting the first loop, and save records to another array. Then you can avoid n^2 queries:


    $emailToRecord = array();
    $select2 = mysql_query("select ...");
    while($row2=mysql_fetch_array($select2)) {
       $emailToRecord[$row2["header"]] = $row2;
    }

    while($row=mysql_fetch_array($select))
    {
        echo "1st";
        $find = $row["email"];
        if (isset($emailToRecord[$find])) {
               echo "found it";
        }
    }

Upvotes: 1

Class
Class

Reputation: 3160

I hope this makes sense.

This is because the first while executes then the second while loops and doesn't end until it is finished, then it finishes and returns to the first while and it returns true (another row) and executes and goes to the next while, but it's pointer to the current 'row' has reached the end so it doesn't execute and it mainly just does the main loop. That's why you get 122222221111111

Upvotes: 0

Related Questions