问题描述
我有一个数据库db1,其中的表内容包含2个字段:表user(userID varchar(1),userName varchar(30)).
I have a database db1 with a table content 2 fields: table user(userID varchar(1), userName varchar(30)).
我在此表中放置了2行:(01,admin)和(02,user)
I put 2 rows in this table: (01,admin) and (02,user)
我想打印出所有用户名,所以我进行了编码:
I want to print all userName out so I code:
$conn=mysqli_connect("localhost","root","",db1);
if($conn)
{
$sql="SELECT userName FROM user";
$resul=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
$mysqli_close($conn);
foreach($row as $a)
{
echo $a;
}
}
但是我得到的结果是 管理员不是 管理员用户您能否告诉我原因,以及如何解决?谢谢!
But the result I get is adminadminnot adminuserCould you please tell me why, and how can I fix it?Thanks!
推荐答案
因为 mysqli_fetch_array
返回第一行,并且您正在foreach
中的同一$row
上循环,您只会从该第一$row
中获取数据.
Because mysqli_fetch_array
returns the first row and that you are looping over the same $row
in your foreach
, you'll only be getting data from that first $row
.
您要使用的是while循环,它在这种情况下更合适:
What you want to do instead is use a while loop which is more suitable in this case:
while ($row=mysqli_fetch_array($result)) {
print_r($row);
}
这篇关于想要打印出mysqli_fetch_array的所有结果,但是它多次返回第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!