Store all query data in an array.
$array = array();
while($row = $result->fetch_assoc()) {
$array[] = $row;
}
//exemplo retornado
//Array ( [0] => Array ( [lamp_estado] => estado AAA [lamp_descricao] => lâmpadas LED ) [1] => Array ( [lamp_estado] =>estado BBB [lamp_descricao] => lâmpadas fluorescentes ) [2] => Array ( [lamp_estado] => estado CCC [lamp_descricao] => lâmpadas tubulares ) .....
And to access array elements:
$estado = $estado2 = $array[1]['lamp_estado'];
echo $estado; //estado BBB
echo $estado2; //estado BBB
$descricao = $descricao2 = $array[1]['lamp_descricao];
echo $descricao; //lâmpadas fluorescentes
echo $descricao2; //lâmpadas fluorescentes
Clearing this problem with ta gravando apenas o último valor da busca
Imagine a table whose column is called "bulbs" and the (->) arrow is the list pointer.
-> lâmpadas LED
lâmpadas fluorescentes
lâmpadas tubulares
When we use the function $result->fetch_assoc()
, it returns the first line:
while($row = $result->fetch_assoc()) {
$estado=$row["lamp_estado"];
At this moment the $ status variable takes the value LED bulbs and the pointer moves forward, and our list goes like this:
>
lâmpadas LED
-> lâmpadas fluorescentes
lâmpadas tubulares
At this point the $ status variable takes the value fluorescent lights and the pointer moves one position forward, and so on.
So that's why in your while in the variable $estado
, as well as the other variables, only the last name appears.
When running while
, the value of the $estado
variable is being overwritten because you are not concatenating the values.
You can for example do so to concatenate
$estado = $estado.",".$row["lamp_estado"];
This would result in a string with the comma-separated data.