Mysql search data Save to PHP variables

3

I want to get the value of a query in mysql

SELECT lamp_estado, lamp_descricao FROM lampadas ORDER BY lamp_id

And record each value found in the search, in a variable. I am using this command but it is only writing the last search value.

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $estado=$row["lamp_estado"];
        $descricao=$row["lamp_descricao"];
        $estado2=$row["lamp_estado"];
        $descricao2=$row["lamp_descricao"];
    }
}
    
asked by anonymous 02.11.2017 / 23:56

2 answers

0

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.

    
03.11.2017 / 03:00
0

This is because you are overlapping the information.

A simple variable can not store separate information. To do this, the variable must be a array , that is, a list of variables.

When you do:

$estado = $row["lamp_estado"];

The variable $estado will receive the value of the lamp_estado column of the current line, if there is a value in the $estado variable, that value will be lost. You solve this information loss problem by creating a array (list) of states:

$estado = array();
$descricao = array();
$estado2 = array();
$descricao2 = array();
while ($row = $result->fetch_assoc()) {
    $estado.push($row["lamp_estado"]);
    $descricao.push($row["lamp_descricao"]);
    $estado2.push($row["lamp_estado"]);
    $descricao2.push($row["lamp_descricao"]);
}

The push command is responsible for adding an item to array , it causes the variable of type array to allocate more space in memory to store a new variable.

For you to write the name of the second state for example, you have to get the contents of the second space of the $estado variable. Since array starts counting spaces in 0 , the position of the second space of the $estado variable is 1 :

echo $estado[1];
    
03.11.2017 / 00:31