title: PHP loop to create an HTML table
body: <p>I have an array which is populated from a MySQL query:</p>
  
  <pre><code>if ($result) {
      $foundResult = true;
      foreach ($result as $row) {
          array_push($searchResultAccount,$row-&gt;Account);
          array_push($searchResultUsername,$row-&gt;Username);
          array_push($searchResultPassword,$row-&gt;Password);
          array_push($searchResultCreated,$row-&gt;Created);
          array_push($searchResultStrength,$row-&gt;Strength);
      }
  } else {
      array_push($error,"No results found");
  }
  </code></pre>
  
  <p>Further down in my page, I want to create a table and populate the bootstrap table with the results from the array. However, each cell in the table prints all sets of results for that given field. I suspect I have a problem with my loop logic. I tried changing from a foreach to a for loop but still no luck:</p>
  
  <pre><code>&lt;tbody&gt;
    &lt;?php for ($x = 0; $x &lt; sizeof($result); $x++) { ?&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;?php for ($i = 0; $i &lt; sizeof($searchResultAccount); $i++){echo($searchResultAccount[$i]);}?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php for ($i = 0; $i &lt; sizeof($searchResultUsername); $i++){echo($searchResultUsername[$i]);}?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php for ($i = 0; $i &lt; sizeof($searchResultPassword); $i++){echo($searchResultPassword[$i]);}?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php for ($i = 0; $i &lt; sizeof($searchResultCreated); $i++){echo($searchResultCreated[$i]);}?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;p&gt;ExpiresHere&lt;/p&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php for ($i = 0; $i &lt; sizeof($searchResultStrength); $i++){echo($searchResultStrength[$i]);}?&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;?php } ?&gt;
  &lt;/tbody&gt;
  </code></pre>
  
  <p>The outcome looks like below:</p>
  
  <p><a href="https://i.stack.imgur.com/B6qB3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B6qB3.png" alt="enter image description here"></a></p>
  
  <p>I have 2 results in the array, and as you can see they're printing together in each cell. Any ideas on where I've went wrong ?</p>
  
  <p>Edit:  Full PHP code as from the query string below:</p>
  
  <pre><code>$sql = "SELECT * FROM Accounts WHERE User_ID = :userid AND Account = :account";
      $stmt = $pdo-&gt;prepare($sql);
  
      $stmt-&gt;bindValue(':userid',$userid);
      $stmt-&gt;bindValue(':account',$account);
      $stmt-&gt;execute();
      $result = $stmt-&gt;fetchAll(PDO::FETCH_CLASS);
  
      if($result) {
          $foundResult = true;
          foreach($result as $row) {
              array_push($searchResultAccount,$row-&gt;Account);
              array_push($searchResultUsername,$row-&gt;Username);
              array_push($searchResultPassword,$row-&gt;Password);
              array_push($searchResultCreated,$row-&gt;Created);
              array_push($searchResultStrength,$row-&gt;Strength);
          }
      } else {
          array_push($error,"No results found");
      }
  </code></pre>