[英]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-else
, while
弄亂了內容。 如果我“漂亮地打印”它上面的代碼片段看起來像這樣(請參閱我的評論):
<?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.