美好的一天,
这个php语句似乎只能从表中返回一行。我如何修改它以便它能带回所有行?
while($row = $result->fetch_array())
{
$array = array("UserID" => $row['UserID'], "runDate" => $row['runDate'],"firstName" => $row['firstName'], "Time" => $row['Time'], "Distance" => $row['Distance']);
}
答案 0 :(得分:3)
更改
$array = array(/** */);
到
$array[] = array(/** */);
就是这样。
答案 1 :(得分:3)
我会用:
while($row = $result->fetch_array()){
$array[] = $row;
}
通过使用$row
变量,它使数组键与数据库列名称相同
答案 2 :(得分:0)
这应该有用(我还没有测试过):
$all_rows = array();
while($row = $result->fetch_array())
{
$all_rows[] = array("UserID" => $row['UserID'], "runDate" => $row['runDate'],"firstName" => $row['firstName'], "Time" => $row['Time'], "Distance" => $row['Distance']);
}
答案 3 :(得分:0)
试试这个:
foreach($result->fetch_array() as $row)
{
$array["UserID"][] = $row['UserID'];
$array["runDate"][] = $row['runDate'];
$array["firstName"][] = $row['firstName'];
$array["Time"][] = $row['Time'];
$array["Distance"][] = $row['Distance'];
}
然后检查出来:
var_dump($array);