gpt4 book ai didi

php - fatal error : Call to a member function fetch_assoc() on a non-object while converting result to array

转载 作者:行者123 更新时间:2023-11-28 23:45:59 27 4
gpt4 key购买 nike

我正在尝试以数组的形式获取多行,然后将其转换为 json

这是我的代码:

public function getTest($standard,$chapid,$count)
{


if($count>0)
{


$stmt = $this->conn->prepare("SELECT question from questions where standard=? and chapterId=? ORDER BY rand()");
$stmt->bind_param("ss", $standard,$chapid);
$result=$stmt->execute();
echo $result;
$stmt->close();
$rows = $this->resultToArray($result);
echo json_encode($rows);// Array of rows



}

else
{

$rows["success"] = 0;
$rows["message"] = "Posts NOT Available!";
}



echo json_encode($rows);


}

public function resultToArray($result) {
$rows["success"] = 1;
$rows["message"] = "Post Available!";
$rows["posts"] = array();
while($row = $result->fetch_assoc()) // <--Getting ERROR OVER HERE
{
array_push($rows["posts"], $row);
}
return $rows;
}

我是 php 的新手,所以我不明白为什么我在 resultToArray() 函数的 while 行中遇到错误。

最佳答案

您需要获取查询结果; fetch(PDO::FETCH_ASSOC) 将每个结果作为关联数组返回:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
array_push($results, $row);
}

关于php - fatal error : Call to a member function fetch_assoc() on a non-object while converting result to array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33572821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com