Gonçalo, based on Gonçalo's answer , but I simplified the loop:
$postos = array();
while($array1 = mysqli_fetch_array($procura1)) $postos[] = $array1['posto'];
This works because when you do variavel[] = 'valor';
the value is added to the end of the array .
Depending on the intended structure, you have an even simpler alternative:
$tudo = mysqli_fetch_all($procura1);
This takes all the results at once, without having to do a loop manually. But in this case, you will get an array of arrays and not values.
If you are running PHP 5.5 or higher, you can use fetch_all
like this:
$tudo = mysqli_fetch_all($procura1, MYSQLI_ASSOC); // Pegamos todas as linhas
$postos = array_column($tudo, 'posto'); // extraimos só a coluna 'posto'
The MYSQLI_ASSOC
is for the columns to be indexed with names instead of numbers. If we use MYSQLI_NUM
it would be necessary array_column($tudo, numero_da_coluna)
, which complicates maintenance somewhat if something is changed in SELECT
.
More details in the manual:
link
link