Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think your <code>$comments</code> array contains what you think it contains.</p> <p>Right now it contains an array for a single database row, presumably a row with 5 columns of data.</p> <p>This is because <code>$comment-&gt;fetch_data($id)</code> will only fetch a single row of data from the database. It is a poorly designed method.</p> <p>As such <code>$comments</code> array looks something like this:</p> <pre><code>[ 0 =&gt; 'field 1 value', 1 =&gt; 'field 2 value', ... 4 =&gt; 'field 4 value', 'field 1 name' =&gt; 'field 1 value', ... 'field 5 name' =&gt; 'field 5 value' ] </code></pre> <p>Note it would have both numeric and associative indexes because you are using default PDO fetch mode (which is usually configured to fetch both numerical and associative index).</p> <p>When you loop on <code>$comments</code> you are actually looping through these values and since you are trying to output data from <code>$comments</code> instead of l<code>$comment</code> which is locally defined for your loop. Because of this, you are actually accessing the associative keys in this <code>$comments</code> array.</p> <p>What you want to do is to change your fetch method to fetch ALL rows in the DB result set. That might look like this:</p> <pre><code>public function fetch_data($post_id){ global $pdo; $query = $pdo-&gt;prepare("SELECT * FROM comments WHERE post_id = ?"); $query-&gt;bindValue(1, $post_id); $query-&gt;execute(); $rows = array(); while($row = $query-&gt;fetch(PDO::FETCH_ASSOC)) { $rows[] = $row; } return $rows; } </code></pre> <p>And then you need to change the echo statements in your loops to actually echo from <code>$comment</code> (the local variable name defined for the loop) rather than <code>$comments</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload