Log in

View Full Version : Simple Query(1 column) to array to variables (not simple to me )



noobster
03-17-2009, 09:51 AM
Hello,

I need just a little push and as i'm new at arrays it would really be helpfull.

I need to get variables with data coming from a simple sqlquery , my sql query returns datas as
product1
product2
product3
...
i can't seem to get
var1 = product1
var2 = product2 ...

all i can do is echo them in a loop but no usable variable.

here is my "cleared" code so far:


<?php
$query = "SELECT `produits`.`nom_prod` FROM `selectmem` Inner Join `produits` ON `selectmem`.`bon1` = `produits`.`id` OR `selectmem`.`bon2` = `produits`.`id` OR `selectmem`.`bon3` = `produits`.`id` ";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
echo $row['nom_prod'];
echo "<br />";
}
echo "THE_END";

?>

my loop works and gets the datas but how to get datas to as many variables?
$var1, $var2 etc..

This or going directly from query to variables of course


thanks for your help

CrazyChop
03-17-2009, 04:58 PM
As you iterate, you have to store each result in an array or work on the returned result within each iteration.




$data = array();

while($row = mysql_fetch_array($result))
{
echo $row['nom_prod'];
array_push($data, $row['nom_prod']);
echo "<br />";
}

echo "<pre>".print_r($data, true)."</pre>";

noobster
03-17-2009, 07:09 PM
Thanks a lot CrazyChop, i'll test that as soon as possible.
I'll travel for work this week so i'm not sure when i'll give feedback but i'll try to understand and adapt :) wich is always better.

Thanks for your time and tip