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
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";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.