Q:

mysqli error : Cannot use object of type mysqli_result as array

What's wrong with my code? 
$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);
}

A:

You are trying to add a variable to an existing object instance as if it were an array:
$result[] = $obj;

Choose a different name for that array
$result = $stmt->get_result();
while ($obj = $result->fetch_object()) {
    $output[] = $obj;
}
echo json_encode($output);

Alternatively, if you don't do anything with $obj:
$output = $result->fetch_all();
echo json_encode($output);

