mysqli错误:无法将mysqli_result类型的对象用作数组



我的代码出了什么问题?

$stmt = $db->prepare("SELECT * FROM allfriend WHERE friendOf = ?");
$stmt->bind_param('s', $userId);
    if($stmt->execute()){
    $result = $stmt->get_result();
    while ($obj = $result->fetch_object()) {
        $result[] = $obj;
    }
    echo json_encode($result);
}

您正试图将变量添加到现有对象实例中,就好像它是一个数组一样:

$result[] = $obj;

为该阵列选择其他名称

$result = $stmt->get_result();
while ($obj = $result->fetch_object()) {
    $output[] = $obj;
}
echo json_encode($output);

或者,如果您不使用$obj:进行任何操作

$output = $result->fetch_all();
echo json_encode($output);
$result = $stmt->get_result();  

$result已用于获取SQL准备语句的result。您的数组使用了相同的名称。尝试使用名称"output[]">

例如:-

 $output[] = $obj;

最新更新