mySQL - select problems, returning arrays

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
SublicK
Forum Newbie
Posts: 4
Joined: Wed Dec 18, 2002 4:33 am

mySQL - select problems, returning arrays

Post by SublicK »

Hey,

I am working on a simple forum script, and am having some trouble.

My posts have a field called 'parent', which is the post id that it belongs to. This way I can have nested posts. I want to output all of the posts (children) belonging to an other post (the parent), but for some reason my query only returns one child.

Code: Select all

// +-------------------------------------------+
// | content_get_children
// +-------------------------------------------+

   function content_get_children ($id)
   {
     $result = mysql_query ("SELECT `id` FROM `sub_content` WHERE `parent` = $id");
     if ($result != false)
     {
       $children = mysql_fetch_assoc ($result);
       return $children;
     }
     else echo "(CONTENT GET CHILDREN)" . error (mysql_error ());
   }


// +-------------------------------------------+
// | content_read
// +-------------------------------------------+

   function content_read ()
   {
     global $content_id;

     $parent_content = content_get ($content_id);
     $return = format_content ($parent_content);
     $children = content_get_children ($content_id);

     if ($children)
       foreach ($children as $child)
       {
         $child_content = content_get ($child);
         $return .= format_content ($child_content);
       } else $return = error ("No Content.");
     return $return;
   }
The content_get_children () function returns the array from the select query, and the content_read () takes each id in the children array and outputs it. I have five rows (posts) in the table, but it only outputs the first child post. Can you see anything wrong here?

Thanks
Beans
Forum Commoner
Posts: 49
Joined: Mon Dec 23, 2002 3:06 am
Location: Manila, Philippines
Contact:

Post by Beans »

Can you show us a sample result and your expected result?
Peter A. Shushpanov
Forum Newbie
Posts: 4
Joined: Mon Dec 23, 2002 3:38 am
Location: Russia
Contact:

Re: mySQL - select problems, returning arrays

Post by Peter A. Shushpanov »

Code: Select all

// +-------------------------------------------+ 
// | content_get_children 
// +-------------------------------------------+ 

   function content_get_children ($id) 
   { 
     $result = mysql_query ("SELECT `id` FROM `sub_content` WHERE `parent` = $id"); 
     while ($child = mysql_fetch_assoc ($result))
     { 
       $childrenї] = $childї'id'];
     } 
     return $children; 
   }
Post Reply