title: PHP MySQL Comment System With Replies
body: <p>I have code that is able to capture the initial comment and the 1st level of replies, but it doesn't seem to capture the reply to a reply. I know that it requires an indefinite code using some form of recursion, but not quite sure how to properly implement it. 
  Here's what my phpMyAdmin table looks like:</p>
  
  <p>id  name    comment reply_id<br>
  1   BigBadProducer1 I love this vst! I use it all the time! 0<br>
  2   DrummaBoy504    Hey, this is Drumma from Drum Squad!    0<br>
  3   Mike Smith  How did you get the vst to sound so good like that...   1<br>
  4   BigBadProducer1 Yes, I learned how to tweak it from YouTube Mike S...   3<br>
  5   SmoothBeatz3    Dude, Ive been looking for a vst like this for a l...   0<br>
  6   FanBoy123   Hey Drumma, when are you going to release a new hi...   2<br>
  7   Mike Johnson    Hey Fanboy123, why are you such a fanboy of Drum S...   6   </p>
  
  <p>Here's the code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt;
      &lt;title&gt;Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;?php
  // Create connection
  $conn = new mysqli('localhost', 'root', 'mypassword', 'commentsystem2');
  
  $sql1 = "SELECT * FROM comments WHERE reply_id = 0";
  $result1 = mysqli_query($conn, $sql1);
  
  while ($comment = mysqli_fetch_array($result1)) {
      $id = $comment['id'];
      $name = $comment['name'];
      $comment = $comment['comment'];
      
  echo '
  &lt;div class="comments" style="position:relative; margin:auto; width:500px; border:1px solid black; margin-top:1px;"&gt;
  &lt;div&gt;'.$name.'&lt;/div&gt;
  &lt;div&gt;'.$comment.'&lt;br&gt;&lt;br&gt;&lt;/div&gt;
  &lt;/div&gt;
  ';
  
  $sql2 = "SELECT * FROM comments WHERE reply_id = $id";
  $result2 = mysqli_query($conn, $sql2);
  while ($reply = mysqli_fetch_array($result2)) {
  $id_reply = $reply['id'];
  $reply_name = $reply['name'];
  $reply_comment = $reply['comment'];
  $reply_id = $reply['reply_id'];
  
  
  echo '
  &lt;div class="replies" style="position:relative; margin:auto; width:500px; border:1px solid black; margin-top:1px;"&gt;
  &lt;div style="width:80%; text-align:center;"&gt;'.$reply_name.' replied to '.$name.'&lt;/div&gt;
  &lt;div style="width:80%; text-align:center;"&gt;'.$reply_comment.'&lt;br&gt;&lt;br&gt;&lt;/div&gt;
  &lt;/div&gt;
  ';
  
   }//end of replies while loop
  
  }//end of comments while loop
  
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>