Possible Duplicate:
function returning only once, why?
my Database structure looks like
id|parent|
1 | 0 |
2 | 0 |
3 | 0 |
4 | 1 |
5 | 4 |
6 | 5 |
I am in need of a function that gets parent(i.e parent=0) for a id as parameter For eg .. get_parent(6)==returns 1 I did some research and found this question
How can I recursively obtain the "parent ID" of rows in this MySQL table?
I tried making this function
function get_parent_id($cid,$found=array())
{
array_push($found,$cid);
$sql="SELECT * FROM tbl_destinations WHERE id=$cid";
$result = mysql_query($sql) or die ($sql);
if(mysql_num_rows($result))
{
while($row = mysql_fetch_assoc($result))
{
$found[] = get_parent_id($row['parent'], $found);
}
}
return $found;
}
I make a call by
$fnd=get_parent_id();
$array_reverse($fnd);
$parent_root=$fnd['0'];
But my method is wrong. Where did I go wrong?