Chemdawg
Chemdawg

Reputation: 69

cannot reset query row after executing while loop

Hi whenever I try to reset row so that I can use same rows for a new column I get error: reset() expects parameter 1 to be array

I am new to PHP and was trying to build a table that outputs data stored in MySQL:

<?php

include ('config.php');

$user = mysqli_query($userDB, "SELECT username, email FROM user") or die(mysqli_error());

?>
 <table width="200" border="1">
  <tbody>
    <tr>
      <th scope="col">Username</th>
      <th scope="col">Email</th>
    </tr>
    <tr>
    <td align = "center">
    <?php while($row = mysqli_fetch_assoc($user)) echo $row["username"]."<br>"; reset($row); ?>
    </td>
    <td align = "center">
    <?php while($row = mysqli_fetch_assoc($user)) echo $row["email"]."<br>" ?>
    </td>
    </tr>
  </tbody>
</table>

Any better way I can do this?

Upvotes: 0

Views: 28

Answers (1)

Felippe Duarte
Felippe Duarte

Reputation: 15131

You can rewrite in a better way:

<table width="200" border="1">
  <thead>
    <tr>
      <th scope="col">Username</th>
      <th scope="col">Email</th>
    </tr>
  </thead>
  <tbody>
    <?php while($row = mysqli_fetch_assoc($user)): ?>
      <tr>
        <td align = "center">
           <?php echo $row["username"]; ?>
        </td>
        <td align = "center">
           <?php echo $row["email"]; ?>
        </td>
      </tr>
    <?php endwhile; ?>
  </tbody>
</table>

Unless you need to put all users in a single <td>

Upvotes: 1

Related Questions