1

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
        }
    }
}
1
  • Have you tried using nested for loops instead of while loops? for(each item in the data){for...} Commented Feb 5, 2013 at 2:31

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

ohhhh, thats why... Is there a way to reset it so it will just go again? Thank you so much, never thought of that
0

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
            }

        }
    }

2 Comments

$select=mysql_query("SELECT email FROM database.tablename"); $select2=mysql_query("SELECT header FROM database2.tablename2");
use Foreach or keep them in the same table? Why do you have two databases?
0

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.

Comments

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.