I just wrote this a few minutes ago...perhaps it will help someone. Searches for all root elements. Also supports multipe child->parent relationships. Supports up to 5 levels.
$query = "SELECT faq_categories_id id FROM faq_cat_relationship WHERE faq_parent_id is Null OR faq_parent_id = ''";
$firstLevel = $this->runQuery($query);
while(list($firstID) = @mysql_fetch_row($firstLevel)){
$firstCategoryName = $this->getCatNameByID($firstID);
$query = "SELECT faq_categories_id FROM faq_cat_relationship WHERE faq_parent_id = '$firstID'";
$secondLevel = $this->runQuery($query);
$catArray[$firstID] = '';
while(list($secondID) = @mysql_fetch_row($secondLevel)){
$secondCategoryName = $this->getCatNameByID($secondID);
$query = "SELECT faq_categories_id FROM faq_cat_relationship WHERE faq_parent_id = '$secondID'";
$thirdLevel = $this->runQuery($query);
$catArray[$firstID][$secondID] = '';
while(list($thirdID) = @mysql_fetch_row($thirdLevel)){
$thirdCategoryName = $this->getCatNameByID($thirdID);
$query = "SELECT faq_categories_id FROM faq_cat_relationship WHERE faq_parent_id = '$thirdID'";
$fourthLevel = $this->runQuery($query);
$catArray[$firstID][$secondID][$thirdID] = '';
while(list($fourthID) = @mysql_fetch_row($fourthLevel)){
$query = "SELECT faq_categories_id FROM faq_cat_relationship WHERE faq_parent_id = '$fourthID'";
$fifthLevel = $this->runQuery($query);
$catArray[$firstID][$secondID][$thirdID][$fourthID] = '';
while(list($fifthID) = @mysql_fetch_row($fifthLevel)){
$catArray[$firstID][$secondID][$thirdID][$fourthID][$fifthID] = '';
}
}
}
}
unset($fourthID, $thirdID, $secondID, $firstID);
}//end while loop
return $catArray;
}