Create variables with database data?

0

I'd like to have a while of a query and go fetch the data and create 3 variables apart with this information. For example:

$sel_exclui=mysql_query("SELECT * from tabela order by id DESC LIMIT 0,3");

I thought about using mysql_fetch_array but I do not know how to use it. If this is the solution, how can I do it?

I know that mysql is deprecated but this project was being done by someone else and I took ownership of the project and to not have to change everything, I have worked with it.

    
asked by anonymous 22.09.2017 / 18:22

2 answers

2

To get the 3 results is simple:

$result = mysql_query("SELECT * from tabela order by id DESC LIMIT 0,3");
$itens = array();
while ($row = mysql_fetch_array($result)) { 
    $itens[$row['id']] = array($row['name'], $row['outro_campo']);   
} 

I put id and name as an example, instead of id and name are the fields of your table.

    
22.09.2017 / 18:41
2

I usually do this:

while ($row=mysql_fetch_array($sel_exclui)) 
{
   $campo_01 = $row['campo_01'];
   $campo_02 = $row['campo_02'];
   $campo_03 = $row['campo_03'];
}

Where the variable $campo_01 receives the value of campo_01 from the table, and so does the same for the $campo_02 and $campo_03 variables.

Note that it is a while , so if the result of select contains more than one row, the values saved in the variables will always be in the last record of the select.     

22.09.2017 / 18:41