0

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?

2
  • You really want a list of usernames in a single <td> and a list of emails in the other? Commented May 15, 2018 at 22:05
  • Why are you doing reset() on the username? If you want to display the list per row then you're doing it wrong. Commented May 16, 2018 at 0:15

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.