使用 if 和 while 显示数据的问题

我有问题显示数据库使用if和while代码我。


我在数据库中有 4 个数据。


我想使用 using 显示所有数据if,但它只显示 1 个数据。


if($Z = mysqli_fetch_assoc($Y)){

  <!-- Data all display -->

} else {


}

而我使用 data with while,数据全部显示


while($Z = mysqli_fetch_assoc($Y)){

    <!-- Data all display -->

}

如何使用 显示所有数据if?


蝴蝶刀刀
浏览 126回答 2
2回答

噜噜哒

这是不可能的。每次通话时间mysqli_fetch_assoc()在你的$Y变量(这是一种资源),则读取下一行。你必须总是在一个循环(读取while或for)使用mysqli_fetch_assoc()。如果您想一次获取所有结果,则必须使用mysqli_fetch_all().编辑:虽然不能直接使用,但mysqli_fetch_assoc()您也可以执行以下操作:<?php$results = [];while($Z = mysqli_fetch_assoc($Y)) {&nbsp; $results[] = $Z;}然后您的所有行都将存储在$results.

慕无忌1623718

如果我没有错,你使用 IF 语句只是为了检查是否有任何提取的行不是。有一种方法可以在代码下方进行此检查。首先检查是否有使用 if 语句获取的行,然后使用 while 显示所有数据。&nbsp;$rowcount=mysqli_num_rows($Y);&nbsp; &nbsp; if($rowcount>0){&nbsp; &nbsp; while($Z = mysqli_fetch_assoc($Y)){&nbsp; &nbsp; &nbsp; &nbsp; <!-- Data all display -->&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; }else{&nbsp; &nbsp;echo "no data found";}
打开App,查看更多内容
随时随地看视频慕课网APP