tags:

views:

52

answers:

5

suppose i have a query

$array = array();
$sql = mysql_query("SELECT * FROM table");
while($row=mysql_fetch_array($sql)){
 $data = $row['data'];
}
$array = $data;

Now how can i get that each $data values out of while in an array() i.e $array

+1  A: 

by adding each $row to array $data
for reference: http://php.net/types.array

Note that there is no such a thing as "while() array". There are just arrays only.

Col. Shrapnel
+7  A: 
$array = array();
$sql = mysql_query("SELECT * FROM table");

while ($row = mysql_fetch_array($sql)) {
    $array[] = $row['data'];
}
John Kugelman
+1  A: 

Your $data variable is being overwritten during each iteration. So you need to turn it into $data[] and $data should be declared at the top, or you can just use $array[] as the other answer suggests, not sure why you put in the $data variable in their in the first place.

Sabeen Malik
+1  A: 

You need to save each iteration object into a "new" object, so your code will look just like:

$array = array();
$sql = mysql_query("SELECT * FROM table");
while($row=mysql_fetch_array($sql)){
   $array[] = $row['data'];
}

please note the line:

   $array[] = $row['data'];

References:

Garis Suero
A: 
$array = array();
$sql = mysql_query("SELECT * FROM table");
while($row=mysql_fetch_array($sql)){
   $array[] = $row['data'];
}

further u can use

$array['data']

and u can also use mysql_fetch_assoc($sql) instead mysql_fetch_array($sql)

Ricky Dang