I need to load result of a mysqli select statement (single field with multiple rows) into an array like:
JavaScript
x
$post = [318,310,323]
As you can see I used the fetch_array()
:
JavaScript
$posts = [];
..
$stmt->bind_param("s", $sessien);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_NUM))
{
array_push($posts, $row);
}
print_r($posts );
$result->free();
$stmt->close();
$customconn->close();
but on printing r the $posts
is creating something like this (looks like an array in array):
JavaScript
Array
(
[0] => Array
(
[0] => 318
)
[1] => Array
(
[0] => 310
)
[2] => Array
(
[0] => 323
)
)
How can I fix this to have something like:
JavaScript
$post = [318,310,323]
Advertisement
Answer
As mysqli_result::fetch_array()
will always return an array – even for 1 field, you need to add that field rather than the entire result to your overall result array…
JavaScript
$row = $result->fetch_array(MYSQLI_NUM)
array_push($posts, $row[0]);