title: Mysql data in inheritance format using PHP
body: <p>I have a table in this order, i am trying to get data from this table where each row depends on the other to get the desired information i need.</p>
  
  <p>ID &nbsp;USERNAME &nbsp; &nbsp;REFFERAL<br />1 &nbsp; &nbsp;user1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10<br />2. &nbsp; user2. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br />3 &nbsp; &nbsp;user3. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2<br />4 &nbsp; &nbsp;user4. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3<br />5. &nbsp; User5. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4<br></p>
  
  <pre><code>$sql = mysqli_query($server, “select refferal from profile where id=‘5’”);
  $row1 = mysqli_fetch_assoc($sql);
  $ref1 = $row[‘refferal’];
  
  $sql2 = mysqli_query($server, “select refferal from profile where id=‘$ref1’”);
  $row2= mysqli_fetch_assoc($sql2);
  $ref2 = $row2[‘refferal’];
  
  $sql3 = mysqli_query($server, “select refferal from profile where id=‘$ref2’”);
  $row3= mysqli_fetch_assoc($sql3);
  $ref3 = $row3[‘refferal’];
  
  $sql4 = mysqli_query($server, “select refferal from profile where id=‘$ref3’”);
  $row4= mysqli_fetch_assoc($sql4);
  $ref4 = $row4[‘refferal’];
  </code></pre>
  
  <p>I would want to get all the usernames of of user4,user3,user2,user1 in one query if possible .. This is what i have tried so far but i don't think its is very efficent.</p>