Php 当查询返回零行时,查询和while循环无法正常工作
当行数>0时,解析行的我的查询和while循环可以正常工作,但当=0时则不能正常工作 当返回的行数为0时,似乎跳过了整个while循环。请参阅注释,其中$activeCount被强制设置为=0,并且不会执行并显示以下错误消息 在下面的代码之前,在数组$errorMessages[]中设置了一条默认错误消息,并在触发器发生时更新和添加该消息。当查询中返回0行时,$error\u NoActives的更新不会发生 这是相关代码Php 当查询返回零行时,查询和while循环无法正常工作,php,mysql,Php,Mysql,当行数>0时,解析行的我的查询和while循环可以正常工作,但当=0时则不能正常工作 当返回的行数为0时,似乎跳过了整个while循环。请参阅注释,其中$activeCount被强制设置为=0,并且不会执行并显示以下错误消息 在下面的代码之前,在数组$errorMessages[]中设置了一条默认错误消息,并在触发器发生时更新和添加该消息。当查询中返回0行时,$error\u NoActives的更新不会发生 这是相关代码 //fetch the user's active posts and
//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的find_行和SQL_CALC_find_行
无论如何,当查询返回0行时,$error\u NoActives语句没有执行。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;
}
while循环仅在有结果时运行。这是正常的,因为如果它试图在没有结果的情况下运行,$row中的所有值都将为null 正确的方法是使用mysql\u num\u行:
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>";
}
就是说,您可以更好地处理错误。您的代码末尾缺少一个双引号……值得指出的是,PHP中的mysql\u xxx已被弃用,不推荐使用。如果可能的话,您应该将代码切换为使用PDO库。Styphon,它可以正常工作。我还学到了一些书中没有的while循环。我很感谢你关于另外两点的建议。非常感谢,尼尔斯,谢谢。你的代码也可以用,但是Styphon先发布了。请查看发布的时间,但不要担心=对不起,是我的错。也许它们是按相反的顺序显示的。
$query = "mysqlquery";
if(mysql_query($query)) {
} else {
//some error code
}