查询返回零行时,查询和while循环无法正常工作

时间:2013-05-29 19:45:53

标签: php mysql

我的查询和解析行的while循环在行数>时正常工作。 0,但不是= 0。

当返回的行数为0时,似乎跳过整个while循环。请参阅$activeCount强制= 0的注释,并且以下错误消息不会执行并显示。

在下面的代码之前,在数组$ errorMessages []中设置了一条默认错误消息,它会更新并添加到触发器发生时。在查询中返回0行时,不会发生$ error_NoActives的更新。

以下是相关代码。

//fetch the user's active posts and their count
$fetch = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM (
    SELECT propertyID, streetAddress, city3, city2 FROM residence.property
    INNER JOIN contact ON residence.contact.ContactID = residence.property.ContactID
    WHERE residence.contact.contactEmailAddress1 ='$contactEmailAddress1' AND activePosting = '1') props,
    (SELECT FOUND_ROWS() AS 'activeCount') actives;")
    or die('<li class=error>Ooops</li>'. mysql_error());

//create HTML li items to show each posting and create jQuery to insert to a div
$rowCount = 0;
while ($row = mysql_fetch_array($fetch)) {
    $activeCount = $row["activeCount"];
    //$activeCount = 0; //test - get default error message, seems next 3+ lines are not executing when query has 0 rows
    if( $activeCount == 0 ) {
    $error_NoActives = "<li>You have $activeCount active postings.</li>";
    //don't get this message with 0 active posts, get default error
    $errorMessages[0] = $error_NoActives;
    } else {
        //$activeCount displays correctly if non-zero
        $error_NumberOfActives = "<ul>Welcome back. You have $activeCount active postings.
        //do stuff using $rowCount. Construct li items.
        $rowCount++;
        }
    }
}

我怀疑我对while循环或MySQL的FOUND_ROWS()和SQL_CALC_FOUND_ROWS都不了解。

无论如何,当查询返回0行时,$ error_NoActives语句没有执行。

2 个答案:

答案 0 :(得分:1)

while循环没有运行,因为没有任何东西可以循环,因为你有0行。如果有超过0行,则在WHILE周围设置if语句;如果有0行,则在else中设置代码。还在欢迎消息中修复了语法错误。

//fetch the user's active posts and their count
$fetch = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM (
    SELECT propertyID, streetAddress, city3, city2 FROM residence.property
    INNER JOIN contact ON residence.contact.ContactID = residence.property.ContactID
    WHERE residence.contact.contactEmailAddress1 ='$contactEmailAddress1' AND activePosting = '1') props,
    (SELECT FOUND_ROWS() AS 'activeCount') actives;")
    or die('<li class=error>Ooops</li>'. mysql_error());
//create HTML li items to show each posting and create jQuery to insert to a div
$rowCount = 0;
if( mysql_num_rows($fetch) > 0 )
{
    while ($row = mysql_fetch_array($fetch)) {
        $activeCount = $row["activeCount"];
        //$activeCount = 0; //test - get default error message, seems next 3+ lines are not executing when query has 0 rows

            //$activeCount displays correctly if non-zero
            $error_NumberOfActives = "<ul>Welcome back. You have $activeCount active postings.";
            //do stuff using $rowCount. Construct li items.
            $rowCount++;
            }
    }
}
else {
    $error_NoActives = "<li>You have $activeCount active postings.</li>";
    //don't get this message with 0 active posts, get default error
    $errorMessages[0] = $error_NoActives;
}

答案 1 :(得分:1)

while循环仅在有结果时才会运行。这是正常运行的,因为如果它试图运行没有结果,那么$ row中的所有值都将为空。

正确的方法就是使用mysql_num_rows:

if(mysql_num_rows($fetch)) {
    while($row = mysql_fetch_assoc($fetch)) {
        $error_NumberOfActives = "<ul>Welcome back. You have $activeCount active postings.
        //Do stuff with $row.
    }
} else {
    $error_NoActives = "<li>You have $activeCount active postings.</li>";
}

您可以从查询中完全删除SELECT FOUND_ROWS()。

另外,另一点值得注意的是你的死亡陈述。如果您只是在测试时使用它,那就没关系。但是不要依赖die语句来获取实时代码。更好的解决方案是:

$query = "mysqlquery";
if(mysql_query($query)) {
} else {
    //some error code
}

那是你可以更好地处理错误。