嵌套的mysqli_query没有给出结果

时间:2015-11-18 18:30:20

标签: php mysql arrays mysqli

在下面的代码中,我需要回显子查询的结果。 当我回复$row['email'].'<br>';时,回声工作正常,我收到了所有的电子邮件。

问题在于:

$resultsub.echo $rowb["email"];

为空

问题:子查询中我做错了什么:$ resultsub?

$result = mysqli_query($conn, "SELECT * FROM wp_mm_email_bounces");

if ($result) {
// output data of each row
while( $row = mysqli_fetch_assoc( $result ) )
        {
            echo $row['email'].'<br>'; // echo works okay
            $resultsub =  mysqli_query($conn, "SELECT * FROM `wp_mm_external_emails` WHERE `email` = '". $row['email'] ."' "); 
            // when I echo this subquery it runs ok. but somehow I get an empty
            while( $rowb = mysqli_fetch_assoc( $resultsub) )
            {
                echo $rowb["email"];
        } else {
            echo "0 $resultsub";
            }
} else {
echo "0 results";
}

2 个答案:

答案 0 :(得分:0)

为什么你不使用INNER JOIN查询?

请参阅以下示例:

$sqlString = "SELECT ebounces.email as bouces_email, ext.email as ext_email FROM wp_mm_email_bounces ebouces INNER JOIN wp_mm_external_emails ext ON ebounces.email = ext.email";
$result = mysqli_query($conn, $sqlString);

if ($result) {
    // output data of each row
    while( $row = mysqli_fetch_assoc( $result ) )
    {
        echo $row['bouces_email'].'<br>'; 
        echo $row["ext_email"];
    }
} 
else {
    echo "0 results";
}

聚苯乙烯。将所需的所有列放在SQL语句中。

我的最爱

答案 1 :(得分:0)

我认为你以某种方式弄乱了if-elsewhile部分。如果我“漂亮地打印”上面的代码片段,它看起来像这样(请参阅我的评论):

<?php
$result = mysqli_query($conn, "SELECT * FROM wp_mm_email_bounces");

if ($result) 
{
    while( $row = mysqli_fetch_assoc( $result ) )
    {
        echo $row['email'].'<br>';
        $resultsub =  mysqli_query($conn, "SELECT * FROM `wp_mm_external_emails` WHERE `email` = '". $row['email'] ."' "); 

        while( $rowb = mysqli_fetch_assoc( $resultsub) )
        {
            echo $rowb["email"];
        } 
        else //Where is the corresponding if?
        {
            echo "0 $resultsub";
        }
    }
} //I added this enclosing bracket to the if-statement

else //If this is the corresponding else of your first if at the beginning, then you never closed it
{
    echo "0 results";
}

?>
相关问题