Create variables with database data?

Asked

Viewed 39 times

0

I wish I had one while of a queryand fetch the data and create 3 variables separately with this information. For example:

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

I thought I’d use mysql_fetch_array but I don’t know how to use it. If that’s the solution, how can I do it?

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

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 example, instead of id and name are the fields of your table.

  • But I would like to store these values in variables to use after off the while

  • I made the change, just assign in the variables

  • But then the variable will always be right?

  • this way you can build an array

  • I did as I said, it worked but I did print_r of the array and it appears to me like this Array ( [68] => Array ( [0] => 68 ) [67] => Array ( [0] => 67 ) [66] => Array ( [0] => 66 ) )

  • You have to put the 'id', 'name' and 'other' according to your bank, to know what is coming from a print_r in $Row.

  • 1

    Problem solved :)

Show 3 more comments

2

I usually do like 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 from the table the value of campo_01, and so it is the same for the variables $campo_02 and $campo_03.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.