![](/img/trans.png)
[英]Ajax post request only prints if there are more than one echo statement
[英]echo statement only returning one
如果沒有類別,我會遇到一些問題,如果有,我需要回顯沒有類別,我需要回顯有類別。 它顯示是否有類別,但如果沒有則不顯示。
<tr>
<?php
$db = dbconnect();
$stmt = $db->prepare("SELECT * FROM discussion_categories");
$stmt->execute();
$result = $stmt->get_result();
while (($row = mysqli_fetch_assoc($result)) == true) {
$CategoryID = $row['CategoryID'];
$Name = $row['Name'];
$Description = $row['Description'];
$Photo = $row['Photo'];
if(!empty($CategoryID['CategoryID'])){
echo "<td>No categories</td>";
} else {
echo "<td colspan='4'><img class='profile-photo' src='" . ROOT_URI . "/uploads/" . $Photo . "'></td>";
echo "<td colspan='4'><a href='discussions.php?view={$CategoryID}'>{$Name}</a></td>";
echo "<td><a href='managecategories.php?delete={$CategoryID}'>delete</a></td>";
}
}
?>
</tr>
這是一個很容易解決的問題。
如果沒有類別,則永遠不會執行 while 循環,因為數據庫結果中沒有行。
首先嘗試檢查行數:
if (mysqli_num_rows($result) == 0) {
//there are no categories
echo('no categories');
} else {
//there are categories
echo('there are categories');
//in case you want to loop through your categories now
while (($row = mysqli_fetch_assoc($result)) == true) {
$CategoryID = $row['CategoryID'];
echo($CategoryID);
//your code
}
}
這應該對你有幫助!
如果沒有類別,則此條件:
while (($row = mysqli_fetch_assoc($result)) == true)
這是一種冗長的寫法:
while ( $row = mysqli_fetch_assoc($result) )
永遠不會為真,因此您將進入循環零次 - $row
永遠不會有“真實”值。
如果我們將您的代碼寫成偽代碼,我們會得到:
inspect each result
if the result has a non-empty CategoryID, echo "No categories"
if the result has an empty CategoryID, echo "There are categories"
end of loop
這兩個 if 檢查是錯誤的,但更重要的是它們在 loop 內。
你的意思可能是這樣的:
set found_results flag to false
inspect each result
if the result has a non-empty CategoryID, set found_results flag to true
perform other operations on the result, or use "break;" to end the loop early
end of loop
if found_results flag is true, echo "There are categories"
if found_results flag is false, echo "No categories"
我會讓你把它翻譯回 PHP。 :)
當然,如果你真的只需要知道是否有結果,你可以通過以下方式寫得更簡潔:
mysqli_num_rows
返回的行mysqli_num_rows
SELECT COUNT(*)
而不是SELECT *
,也可能使用WHERE CategoryId IS NOT NULL
子句如果你沒有結果,那么它甚至不會進入 while 循環,所以你的條件語句是多余的(這就是你沒有輸出的原因)。
你最好,正如一些評論中提到的,在你嘗試對結果做任何事情之前檢查結果。
if($result->num_rows > 0) {
// you now know there are results
while ($row = mysqli_fetch_assoc($result)) {
// do your business in here as you would have, but you dont need to worry about nothing to process
}
} else {
// do something in here to send back a null result, or whatever you like
}
while (($row = mysqli_fetch_assoc($result)) == true)
根據以上部分代碼。 如果有任何數據返回或從數據庫中獲取,它只會進入 while 循環,否則如果沒有獲取數據,它將不會進入循環。
如果沒有類別,那么它將退出循環,所以這個echo "<td>No categories</td>";
永遠不會顯示。
此外,由於您已經放了一個回聲說“無類別”,我認為這意味着如果沒有類別,您想要回顯輸出。 但是你的 if 條件是錯誤的,因為如果你想檢查某個變量是否為空,你需要做如下
if(empty($CategoryID['CategoryID'])){
echo "<td>No categories</td>";
} else {
echo "<td colspan='4'><img class='profile-photo' src='" . ROOT_URI . "/uploads/" . $Photo . "'></td>";
echo "<td colspan='4'><a href='discussions.php?view={$CategoryID}'>{$Name}</a></td>";
echo "<td><a href='managecategories.php?delete={$CategoryID}'>delete</a></td>";
}
if(empty())
為空時為真, if(!empty())
非空時為真。
如果我理解正確,您應該使用 empty() 而不是 !empty()。 代碼運行如下:
if(!empty($CategoryID['CategoryID'])){ //if cat is not empty it says no cat
//notice the ! in front of empty tag
echo "<td>No categories</td>";
} else {
echo "<td>There are categories</td>";
}
根據您的代碼,如果類別為空,則會顯示有類別。
通過調用 While 循環,您就是說,如果查詢在結果中至少返回一個類別。
因此,當沒有類別時,循環會混淆。 嘗試用大於零的 CategoryID 計數替換 empty() ,看看會發生什么。
如果 > 0
存在
別的
不存在
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.