0

我的查询和解析行的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 语句不会执行。

4

2 回答 2

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;
}
于 2013-05-29T19:48:01.230 回答
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 声明。如果您只是在测试时使用它,那没关系。但是不要依赖 die 语句来获取实时代码。更好的解决方案是:

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

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

于 2013-05-29T19:52:37.170 回答