Running while loop to display all rows, but it only displays one row
<p>I am trying to execute a while loop to display data regarding flagged posts. I am currently logged in as Conor, who has flagged 2 posts (2 rows in the <code>flagged_posts</code> field). But when I execute the below code, only one div is echo's, whereas two should be echo's as he has flagged two posts.</p>

<p>Here is my approach:</p>

<pre><code>&lt;?php
// getting id of logged in user, need this to see all the flagged posts from logged in user.
$get_uid = mysqli_query ($connect, "SELECT id FROM users WHERE username = '$user' ");
        $get_d = mysqli_fetch_assoc($get_uid);
            $user_ident = $get_d ['id'];

// using the above id and getting all the flagged posts which have been flagged by the id obtained
$flagged_by2 = mysqli_query ($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id = '$user_ident'");
        $num_of_flagged_posts = mysqli_num_rows ($flagged_by2);
        while ($getting_d = mysqli_fetch_assoc ($flagged_by2)){
            $flagged_thought_id = $getting_d['thought_id'];
        }

$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id='$flagged_thought_id' ORDER BY id DESC LIMIT {$start}, {$limit}");
while ($row = mysqli_fetch_array($query)) {
    $thought_id      = $row['id'];
    $message_content = $row['message'];
    $date_of_msg     = $row['post_details'];
    $thoughts_by     = $row['added_by'];
    $attachent       = $row['attachment'];
    $shared          = $row['shared'];

    echo 
        "   &lt;div class='message_wrapper'&gt;
                &lt;p&gt; Details would be displayed here &lt;/p&gt;
            &lt;/div&gt;";
} //while closed
</code></pre>

<p><strong>Edit</strong>:</p>

<p>Pagination (with Marcin Nabiałek approach):</p>

<pre><code>        $flagged_by2 = mysqli_query ($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id = '$user_ident'");
        $flagged_ids = [];  
        $num_of_flagged_posts = mysqli_num_rows ($flagged_by2);
        while ($getting_d = mysqli_fetch_assoc ($flagged_by2)){
            $flagged_ids[] = $getting_d['thought_id'];
        }
        if (!$flagged_ids) {
            $flagged_ids[] = 0;
        }


            // Pagination - Load more content when bottom of page hit.  
            $limit = 10;

            $page = (int) (!isset($_GET['p'])) ? 1 : $_GET['p'];
            # sql query
            $query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id IN " . implode(', ', $flagged_ids)." ORDER BY id DESC");
            # find out query start point
            $start = ($page * $limit) - $limit;
            # query for page navigation
            if(mysqli_num_rows($query) &gt; ($page * $limit) ){
              $next = ++$page;
            }
            //$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id='$flagged_thought_id' ORDER BY id DESC LIMIT {$start}, {$limit}");
$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id IN " . implode(', ', $flagged_ids)." ORDER BY id DESC LIMIT {$start}, {$limit}");
            if (mysqli_num_rows($query) &lt; 1) {
              header('HTTP/1.0 404 Not Found');
              echo "&lt;div class='nothing_to_say'&gt;
                        $ufirstname has no flagged posts.
                    &lt;/div&gt;";
              exit();
            }
</code></pre>

<p>With the above, I get two of the same errors:</p>

<p>mysqli_num_rows() expects parameter 1 to be mysqli_result - On line:</p>

<pre><code>if(mysqli_num_rows($query) &gt; ($page * $limit) ){ 
</code></pre>

<p>AND</p>

<pre><code>if (mysqli_num_rows($query) &lt; 1) {
</code></pre>

<p>If I comment these two lines out, then I get another error:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, on line: </p>

<pre><code>while ($row = mysqli_fetch_array($query)) {
</code></pre>