我的查询和while循环解析行正确工作时,行数> 0,但不=0时。
当返回的行数为0时,似乎跳过了整个while循环。请参阅$activeCount
强制=0的注释,并且不会执行并出现以下错误消息。
在下面的代码之前,在数组$errorMessages[]中设置了一个默认的错误消息,并在触发器发生时更新和添加它。当查询中返回0行时,不会对$ error_noactivities进行更新。
相关代码
//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_noactivities语句不会执行。
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
}
这样可以更好地处理错误
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;
}