title: How to get parent id(root parent) from child id
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/13348229/function-returning-only-once-why">function returning only once, why?</a>  </p>
  </blockquote>
  
  
  
  <p>my Database structure looks like </p>
  
  <pre><code>id|parent|
  1 |   0  |
  2 |   0  |
  3 |   0  |
  4 |   1  |
  5 |   4  |
  6 |   5  |
  </code></pre>
  
  <p>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</p>
  
  <p><a href="https://stackoverflow.com/questions/4006974/how-can-i-recursively-obtain-the-parent-id-of-rows-in-this-mysql-table">How can I recursively obtain the &quot;parent ID&quot; of rows in this MySQL table?</a></p>
  
  <p>I tried making this function </p>
  
  <pre><code>    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;
         }
  </code></pre>
  
  <p>I make a call by </p>
  
  <pre><code>$fnd=get_parent_id();
  $array_reverse($fnd);
  $parent_root=$fnd['0'];
  </code></pre>
  
  <p>But my method is wrong. Where did I go wrong?</p>