猿问

queryResults 和其后的多个 if 语句

在我的 php 代码中,我显示了数据库的结果,一切都很好。现在我添加了一行文本,显示返回的行数。我不喜欢它显示“有 1 个结果”和“有 5 个结果”等。我做了一个 if 语句“如果 $queryResult = 1”然后回显“有 1 个结果”,还有 "if $queryResult > 1" echo "There are xx results",然后 "if $queryResult > 0" 回显所有结果行。


现在,这是代码,除了在只有 1 个结果时使用不正确的“are”和“results”外,它可以工作:


$queryResult = mysqli_num_rows($result);

echo "There are ".$queryResult." results.";

if ($queryResult > 0) 

{ // output data of each row

while($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 row

while($row = mysqli_fetch_assoc($result)) 

{ // Display each field of the records.

它打破了页面,出现以下错误:语法错误,意外的'while'(T_WHILE)我敢肯定还有更多错误,我敢肯定我错过了关于第一个if和elseif语句的一些东西,但是什么是吗?我究竟做错了什么?


我还在花括号中包含了第一个 if 和 elseif 语句,但这也不起作用。


我知道使用“is”和“are”之间的区别是微不足道的,但这是在一个网站上为学习英语作为第二语言的人使用的,这意味着它应该正确使用英语。并且“有 1 个结果”对于新学习者来说并不是很好的英语。


江户川乱折腾
浏览 144回答 1
1回答

慕田峪7331174

您的代码中有一些语法错误,其中之一是您将您设置$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.}
随时随地看视频慕课网APP
我要回答