在我的 php 代碼中,我顯示了數(shù)據(jù)庫的結(jié)果,一切都很好?,F(xiàn)在我添加了一行文本,顯示返回的行數(shù)。我不喜歡它顯示“有 1 個結(jié)果”和“有 5 個結(jié)果”等。我做了一個 if 語句“如果 $queryResult = 1”然后回顯“有 1 個結(jié)果”,還有 "if $queryResult > 1" echo "There are xx results",然后 "if $queryResult > 0" 回顯所有結(jié)果行?,F(xiàn)在,這是代碼,除了在只有 1 個結(jié)果時使用不正確的“are”和“results”外,它可以工作:$queryResult = mysqli_num_rows($result);echo "There are ".$queryResult." results.";if ($queryResult > 0) { // output data of each rowwhile($row = mysqli_fetch_assoc($result)) { // Display each field of the records.所以,我嘗試了這段代碼:$queryResult = mysqli_num_rows($result);//echo "There are ".$queryResult." results.";if ($queryResult = 1) {echo "There was ".$queryResult." lesson found.";}elseif ($queryResult > 1) {echo "There were ".$queryResult." lessons found.";}else ($queryResult > 0) { // output data of each rowwhile($row = mysqli_fetch_assoc($result)) { // Display each field of the records.它打破了頁面,出現(xiàn)以下錯誤:語法錯誤,意外的'while'(T_WHILE)我敢肯定還有更多錯誤,我敢肯定我錯過了關(guān)于第一個if和elseif語句的一些東西,但是什么是嗎?我究竟做錯了什么?我還在花括號中包含了第一個 if 和 elseif 語句,但這也不起作用。我知道使用“is”和“are”之間的區(qū)別是微不足道的,但這是在一個網(wǎng)站上為學(xué)習(xí)英語作為第二語言的人使用的,這意味著它應(yīng)該正確使用英語。并且“有 1 個結(jié)果”對于新學(xué)習(xí)者來說并不是很好的英語。
1 回答

慕田峪7331174
TA貢獻(xiàn)1828條經(jīng)驗 獲得超13個贊
您的代碼中有一些語法錯誤,其中之一是您將您設(shè)置$queryResult為1:
if ($queryResult = 1)
你要:
if ($queryResult == 1)
但是,像這樣壓縮代碼更容易:
$rsltCount = mysqli_num_rows($result);
echo $rsltCount === 1 ? "There is 1 result." : "There are ".$rsltCount." results.";
while($row = mysqli_fetch_assoc($result))
{
// Display each field of the records.
}
- 1 回答
- 0 關(guān)注
- 132 瀏覽
添加回答
舉報
0/150
提交
取消