PHP Comment System With Replies

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:

Here’s the code:

<?php
$conn = new mysqli('localhost', 'root', 'Jordan123', 'commentsystem2');

$sql1 = "SELECT * FROM comments WHERE r_to = 0";
$result1 = $conn->query($sql1);

   while($row1 = $result1->fetch_assoc()) {
       $c_id = $row1['id'];
       $c_name = $row1['name'];
       $c_comment = $row1['comment'];

       echo '
       <div class="comments" style="position:relative; margin:auto; width:25%;">
          <div>ID# '.$c_id.'</div>
          <div style="font-weight:bold;">'.$c_name.'</div>
          <div>'.$c_comment.'<br><br></div>
       </div>
       ';

       $sql2 = "SELECT * FROM comments WHERE r_to = $c_id";
       $result2 = $conn->query($sql2);
       
          while($row2 = $result2->fetch_assoc()) {
              $r_id = $row2['id'];
              $r_name = $row2['name'];
              $r_comment = $row2['comment'];
              $r_to = $row2['r_to'];
              echo '
              <div class="comments" style="position:relative; margin:auto; width:25%; padding-left:80px;">
                 <div>ID# '.$r_id.'</div>
                 <div style="font-weight:bold;">'.$r_name.' replied to '.$c_name.'</div>
                 <div>'.$r_comment.'<br><br></div>
              </div>
              ';
       
          }//end of 1st while loop that captures comments.


   }//end of 1st while loop that captures comments.

    
$conn->close();
?>

Notice how some of the replies to replies that are in the table, are missing on output.

kgkpgkgknk;wgkpwkg;kogowkgwpkgwmgkgkekg wpkpwkt

You’re going about it the wrong way.
First you should identify what type of relationship you have.

A one to one relationship would be each account had a maximum of 1 user.
A one to many relationship would be 1 account had more than 1 user.
But you have a many to many relationship. Meaning, you have many posts that can be used by many users.

That usually requires 3 tables.
I would recommend these tables:
user, post, user_post (juncture table)

user table should have:
user_id, name

post table should have:
post_id, title

user_post should have:
user_id, post_id, comment