0

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);
}
0

2 Answers 2

0

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);
Sign up to request clarification or add additional context in comments.

Comments

0
$result = $stmt->get_result();  

The $result is already used to get the RESULT of the SQL prepared statement. You have used the same name for your array. Try to use the name 'output[]'

eg:-

 $output[] = $obj;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.