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

Php mysqli错误:无法将mysqli_result类型的对象用作数组,php,Php,我的代码怎么了 $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[]

我的代码怎么了

$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已用于获取SQL准备语句的结果。 您对数组使用了相同的名称。 尝试使用名称“output[]”

例如:-

$result = $stmt->get_result();  
 $output[] = $obj;