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

4 回答 4

1

您需要在进入内部循环之前执行第二个查询:

    而($row=mysql_fetch_array($select))
    {
        回声“第一”;

        $select2 = mysql_query("选择 ...");

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

更好的是在开始第一个循环之前运行第二个查询,并将记录保存到另一个数组。然后你可以避免 n^2 查询:

    $emailToRecord = 数组();
    $select2 = mysql_query("选择 ...");
    而($row2=mysql_fetch_array($select2)) {
       $emailToRecord[$row2["header"]] = $row2;
    }

    而($row=mysql_fetch_array($select))
    {
        回声“第一”;
        $find = $row["email"];
        if (isset($emailToRecord[$find])) {
               echo "找到了";
        }
    }

于 2013-02-05T02:36:29.350 回答
0

除非它在两个查询中的行数相同,否则它当然不会发布相同的内容。
您可以添加查询($select 和 $select2)吗?

有人可以告诉我如何发表评论而不是答案吗?

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

        }
    }
于 2013-02-05T02:25:41.397 回答
0

我希望这是有道理的。

这是因为第一个 while 执行然后第二个 while 循环并且直到它完成才结束,然后它完成并返回到第一个 while 并且它返回 true(另一行)并执行并进入下一个 while,但它是指向当前“行”的指针已到达末尾,因此它不会执行,它主要只是执行主循环。这就是为什么你得到 122222221111111

于 2013-02-05T02:32:49.897 回答
0

不要mysql_fetch_array在另一个 while 循环中运行。首先提取所有数据并比较两个表的结果。

$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.
于 2013-02-05T02:33:44.140 回答