无法返回所需值,查询和循环不断返回 i

你好,我试图将查询的结果放入一个数组中,但它不起作用 intead 它只显示 i


<?php

$sqlo = mysqli_query($conn, "SELECT * FROM users");

$i=1;

while ($h=mysqli_fetch_assoc($sqlo)) {

    echo "<br>counter[i] : ".$counter[$i] = $h['username'];

    echo "<br>i++ : ".$i++;

}

?>


繁花不似锦
浏览 113回答 2
2回答

PIPIONE

在循环之前创建数组变量。$i=1;$counter[];while ($h=mysqli_fetch_assoc($sqlo)) {&nbsp; &nbsp; echo "<br>counter[i] : ".$counter[$i] = $h['username'];&nbsp; &nbsp; eecho "<br>i++ : ".$i++;}

FFIVE

由于建议将分配与回声分开,Barmar 建议。试试这个代码:<?php$sqlo = mysqli_query($conn, "SELECT * FROM users");$i=1;$counter = [];while ($h=mysqli_fetch_assoc($sqlo)) {&nbsp; &nbsp; $counter[$i] = $h['username']; //if you need to store username inside an array&nbsp; &nbsp; echo "<br>counter[i] : ".$counter[$i];&nbsp; &nbsp; echo "<br>i++ : ".$i++;}
打开App,查看更多内容
随时随地看视频慕课网APP