3

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

5 Answers 5

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

while ($row = mysql_fetch_array($sql)) {
    $array[] = $row['data'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

1

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:

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.