繁体   English   中英

嵌套的mysqli_query没有返回结果

[英]nested mysqli_query gives no results back

在下面的这段代码中,我需要回显子查询的结果。 当我回显$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";
}

为什么不使用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语句中。

我最好的

我认为您以某种方式弄乱了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";
}

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM