[SOLVED] data from mysql_fetch_array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
patejl
Forum Newbie
Posts: 3
Joined: Mon Nov 24, 2003 2:35 am
Contact:

[SOLVED] data from mysql_fetch_array

Post by patejl »

hey all,
for data collecting and printing from mysql i'm using mysql_fetch_array statement. e.g.

while ($area=mysql_fetch_array($result)){
print $area["name"];
}

$area is acitve only in while loop. how to store whole arrary for later use? when while {} is over $area not exist.

hope you understand me ;)
p.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You need to save the data to another array:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
    $area['name'][] = $row['name'];
}

// then later you can do something like
echo '<ul>';
foreach ($area['name'] as $name) {
    echo '<li>'.$name.'</li>';
}
echo '</ul>';
Mac
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Code: Select all

<?php
echo '<ul>'; 
foreach ($area['name'] as $name) { 
    echo '<li>'.$name.'</li>'; 
} 
echo '</ul>';
?>

or you could also do this :

Code: Select all

<?php
$name = array();
while ($row = mysql_fetch_assoc($result))
{
     $name[] = $row['name'];
}

// and call it like this 

for($i=0; $i<=count($name); $i++)
{
    echo $name[$i];
}

?>
patejl
Forum Newbie
Posts: 3
Joined: Mon Nov 24, 2003 2:35 am
Contact:

Post by patejl »

great. so easy :)
thx a lot...

p.
Post Reply