【发布时间】:2018-02-01 23:29:49
【问题描述】:
我必须在单行中获取结果,但我不能运行多行。我怎样才能解决这个问题?
$stmt = $this->conn->prepare("SELECT * from user where id=?");
$stmt->bind_param("s", $id);
if($stmt->execute()){
$result = $stmt->get_result()->fetch_array(MYSQLI_ASSOC);
$stmt->close();
return $result;
}
我得到了像这样明智的结果
{"ID":2,"Name":"Anju"}
但我需要获取所有用户结果。我的代码在这里
$stmt = $this->conn->prepare("SELECT * from user where id=?");
$stmt->bind_param("s", $id);
if($stmt->execute()){
$result = array();
while ($row = $stmt->get_result()->fetch_array(MYSQLI_ASSOC)) {
$result[] = $row;
}
$stmt->close();
return $result;
}
我收到了错误
致命错误:在第 5 行对非对象调用成员函数 fetch_array()
该行是:
while ($row = $stmt->get_result()->fetch_array(MYSQLI_ASSOC))
我的预期结果是
{"ID":1,"Name":"Obi"}, {"ID":3,"Name":"Oman"}, {"ID":4,"Name":"Anju"}
【问题讨论】: