I have a query like this:
$stmt = $cxn->prepare('SELECT post_id, user_id, content, datetime, total_comments, total_likes FROM posts WHERE user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$stmt->bind_result($post_id, $user_id, $content, $datetime, $total_comments, $total_likes);
$stmt->fetch();
$stmt->close();
I'd like to instead just have the results put directly into an array. How can I change my code to reflect that (PHP and MySQLi)?
Thanks.
For a normal array (indexed by numbers), you can use $stmt->fetch_array()
That will look like this: [ ['x', 'y'], ['a', 'b'] ]
When you need associative arrays, this will do: $stmt->fetch_assoc()
and it will look like this: [ ['field1' => 'x', 'field2' => 'y'], ['field1' => 'a', 'field2' => 'b'] ]