Skip to content
Advertisement

Populating MySQLi result into a PHP indexed array

I need to load result of a mysqli select statement (single field with multiple rows) into an array like:

JavaScript

As you can see I used the fetch_array():

JavaScript

but on printing r the $posts is creating something like this (looks like an array in array):

JavaScript

How can I fix this to have something like:

JavaScript

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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement