开发者

passing while() array to a variable out of while() in php

开发者 https://www.devze.com 2023-01-14 17:49 出处:网络
suppose i have a quer开发者_运维技巧y $array = array(); $sql = mysql_query(\"SELECT * FROM table\");

suppose i have a quer开发者_运维技巧y

$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


$array = array();
$sql = mysql_query("SELECT * FROM table");

while ($row = mysql_fetch_array($sql)) {
    $array[] = $row['data'];
}


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.


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.


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:

  • PHP Arrays


$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)

0

精彩评论

暂无评论...
验证码 换一张
取 消