title: PHP iterate through every row
body: <p>I'm trying to display the information from every row in my database. I have multiple rows with information that I'm going to use to output to my page.</p>
  
  <p>Here's what my table looks like:</p>
  
  <pre><code>id | collection | name | image
  -------------------------------
   1 |  Col One   |col-1 | url-1
  -------------------------------
   2 |  Col Two   |col-2 | url-2
  </code></pre>
  
  <p>Here's what my code currently looks like:</p>
  
  <pre><code>$sql = "SELECT * FROM collections";
  $result = $link-&gt;query($sql);
  $collections = mysqli_fetch_array($result, MYSQLI_BOTH);
  
  $html = '&lt;div class="collection" id="' . $collections["name"] . '"&gt;&lt;div class="collection-hover"&gt;' . $collections["collection"] . '&lt;/div&gt;&lt;/div&gt;';
  echo '&lt;div id="collectionsDiv"&gt;';
  while ($row = $result-&gt;fetch_assoc()) {
      echo $html;
  }
  echo '&lt;/div&gt;';
  </code></pre>
  
  <p>My code currently puts only the first row into the $collection variable, and it echoes everything correctly for that first row. But, I need every row to print to the page. I've found mysqli_fetch_all, and when I do a var_dump, I get all of the rows. However, my while loop breaks and I get "undefined index" errors for "name" and "collection" in my $html variable line. </p>
  
  <p>As per answers I've found on this site, I've tried these loops:</p>
  
  <pre><code>while($collections = mysqli_fetch_array($result)) {
      $summary = $collections['collection'];
  echo $summary;
  }
  
  while($row = mysqli_fetch_assoc($result)) {
      var_dump($row);
  }
  
  $rows = new MySqlResult($collections);
  foreach ($rows as $row) {
      $summary = $row['collection'];
      echo $summary;
  }
  </code></pre>
  
  <p>answers found here:
  <a href="https://stackoverflow.com/questions/14109648/mysql-display-multiple-rows">MySQL Display Multiple Rows</a> &amp;
  <a href="https://stackoverflow.com/questions/4372197/mysql-returns-only-one-row">MySQL returns only one row</a> &amp; <a href="https://stackoverflow.com/questions/18186831/php-mysqli-fetch-all-iterate-through-each-row/18186932#18186932">PHP mySQLi_fetch_all: iterate through each row</a></p>
  
  <p>The first two only return the last row, and the last one doesn't work (I think the MySqlResult may have depreciated. I tried mysqli_result in its place, but it didn't work either). </p>
  
  <p>I'm not sure what piece of the puzzle I'm missing.</p>
  
  <p>If I were to use mysqli_fetch_all, how could I get all of the information for each item? Is there a better way to do this? Maybe I could pass the fetch_all array ID in the $html variable as well, but I'm not sure how I would do that either.</p>
  
  <p>I'm very new to PHP, so pardon me if this is really simple. </p>