-2

I'm in need of some help. I'm working in dreamweaver and I'm trying to insert values from my MySQL database into a table on my HTML page.

Dreamweaver generated these variables for me from the server behaviours

mysql_select_db($database_connection, $connection);
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
$row_mwAcc = mysql_fetch_assoc($mwAcc);
$totalRows_mwAcc = mysql_num_rows($mwAcc);

Now what I need help with is what to put into the while loop for my PHP script, this is what I have so far

<table class="table table-bordered">
    <?php while (): ?>
        <tr>
            <td><?php echo $row['id'] ?></td>
        </tr>
    <?php endwhile; ?>
</table>
Jose Rojas
  • 3,490
  • 3
  • 26
  • 40

2 Answers2

0

With an update to use PDO you can change the while loop into a foreach on the query result. PDO should be used over mysql_ interface methods, due to deprecation: Why shouldn't I use mysql_* functions in PHP?

<?php
  $dbh = new PDO('mysql:host=...;dbname=...', $user, $pass);
  $results = $dbh->query('SELECT * FROM accounts');
?>
<table class="table table-bordered">
    <?php foreach ($results as $row): ?>
        <tr>
            <td><?php echo $row['id'] ?></td>
        </tr>
    <?php endforeach; ?>
</table>
joncloud
  • 757
  • 5
  • 14
  • Thank you! for some reason it skips the first entry. I have 6 entries in the database but it starts at 2 and ends at 6. Any advice? – DEATH-SKILL Apr 17 '16 at 03:56
  • Make sure that `mysql_fetch_assoc` is only being called in the `while` loop. In the previous code snippet it was being called above the table block. – joncloud Apr 17 '16 at 03:58
  • 1
    Do not, I repeat, DO NOT, use the mysql_* interface. Switch to mysqli or PDO. – Rick James Jun 05 '17 at 01:40
0

You need to write your fetch command within the while loop itself.

   $query_mwAcc = "SELECT * FROM accounts";

   $mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());

   while($row_mwAcc = mysql_fetch_assoc($mwAcc)) { 
?>      

<tr>
   <td><?php echo $row_mwAcc['id'] ?></td>
</tr>

<?php } ?>
</table>
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32