Jump to content

manipulating mysql_fetch_array


slanton

Recommended Posts

Say I had a table like

Fred dog

Paul cat

Mark cat

Fred fish

Paul dog

and used a query like

//$query = "SELECT name, pet FROM clients";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
extract($row);
echo $name;
echo $pet;
}

I would get each row as

Fred dog,

Paul cat,

Mark cat

Fred fish

Paul dog

 

What I am trying to get is an array for each name something like

Fred dog fish,

Paul cat dog,

Mark cat.

I can't figure it out! Anyone?

Link to comment
https://forums.phpfreaks.com/topic/96242-manipulating-mysql_fetch_array/
Share on other sites

try

//$query = "SELECT name, pet FROM clients";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
extract($row);
//echo $name;
//echo $pet;
$org_arr[$name][] = $pet;
}
foreach ($org_arr as $name => $pets){
echo $name, ' - ', implode(', ', $pets), "<br />\n";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.