title: How can I use recursion to retrieve parent nodes?
body: <p>I have created a recursive function to get parent products of a product. I'm almost sure I'm nearly there, but need help snapping out of recursion.</p>
  
  <p>The result I am looking for is something like:</p>
  
  <pre><code>Product 1 (id:1, parent:none)
  
  Product 2 (id:2, parent:1)
  
  --- --- Product 3 (id:3, parent:2)
  
  --- --- Product 4 (id:4, parent:2)
  
  --- Product 5 (id:5, parent:1)
  
  --- --- Product 6 (id:6, parent:5)
  
  --- --- Product 7 (id:7, parent:5)
  </code></pre>
  
  <p>My updated function is as follows:</p>
  
  <pre><code>function get_parents ($pid, $found = array()) {
      array_push ($found, $pid);
  
      $sql = "SELECT * FROM products WHERE child_id = '$pid'";
      $result = mysql_query($sql) or die ($sql);
  
      if(mysql_num_rows($result)){
          while($row = mysql_fetch_assoc($result)){
              $found[] = get_parents($row['pid'], $found);
          }
      }
      return $found;
  }
  </code></pre>
  
  <p>I call it using a simple:</p>
  
  <pre><code>$parents = get_parents($pid);
  </code></pre>
  
  <p>The problem I am having is that when I run it, it creates an infinite loop, which doesn't break.</p>
  
  <p>I don't want to get done for spamming so I have saved the result of my array to a text file, which can be seen here <a href="http://vasa.co/array.txt" rel="nofollow">http://vasa.co/array.txt</a></p>
  
  <p>Any help would be seriously appreciated :-)</p>