我正在尝试为我的表单获取parentId
的所有行。但是我的下面的代码不能只将1个记录提取到我的数组中:
public function getChildByParent($parentId)
{
$stmt = $this->conn->prepare("SELECT childId, nick, relation FROM childId WHERE parentId = ?");
$stmt->bind_param("s", $parentId);
$stmt->execute();
$stmt->bind_result($childId, $nick, $relation);
$stmt->fetch();
$user = array();
$user['childId'] = $childId;
$user['nick'] = $nick;
$user['relation'] = $relation;
return $user;
}
我知道我需要将$stmt->fetch()
和$user = array()
调整到fetch_all。你能帮我解决一下这段代码吗?
感谢您的努力。
答案 0 :(得分:1)
使用$stmt->get_result()
设置$result->fetch_all()
以在一次通话中获取所有记录。
<强>尝试:强>
public function getChildByParent($parentId)
{
$stmt = $this->conn->prepare("SELECT childId, nick, relation FROM childId WHERE parentId = ?");
$stmt->bind_param("i", $parentId);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $user;
}