我的查询和解析行的while循环在行数> 0时正常工作,但在= 0时不能正常工作。
当返回的行数为 0 时,似乎跳过了整个 while 循环。请参阅强制 =0 的注释,$activeCount并且不会执行并出现以下错误消息。
在下面的代码之前,在数组 $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 语句不会执行。