Loes
Loes

Reputation: 25

Retrieve data from MySQL into HTML table

I am trying to get information from my MySQL database into HTML table but it seems not to work and I have no clue where's the mistake. Could someone check the code and tell me what I am doing wrong?

   <?php

require('db.php');

$sql = mysql_query("SELECT * FROM tablename ORDER BY id ASC");

$id = 'ID';
$fullname = 'fullname';
$password = 'password';
$adres = 'adres';
$telephone = 'telephone';
$registration = 'registration';
while ($rows = mysql_fetch_assoc($sql)){ 

}
?>
<table width="100%" border="1">
  <thead><tr>
    <td>Klantennummer</td>
    <td>Volledige naam</td>
    <td>Email</td>
    <td>Adres</td>
    <td>Klant bewerken</td>
    <td>Klant verwijderen</td>
  </tr>
  </thead>
  <tbody>
<?php
          while( $rows = mysql_fetch_assoc($sql) ){
            echo
            "<tr>
              <td>{$rows\['id'\]}</td>
              <td>{$rows\['fullname'\]}</td>
              <td>{$rows\['email'\]}</td>
              <td>{$rows\['adres'\]}</td>

            </tr>\n";
          }
        ?>
      </tbody>
    </table>

Upvotes: 1

Views: 5209

Answers (5)

Hasan Tareque
Hasan Tareque

Reputation: 1741

You have one unnecessary while loop which i removed. Also your html table header got 6 items while you are printing 4 items in your table. If your database connection is works fine then following code should work for you...

<?php
        require('db.php');
        $sql = mysql_query("SELECT * FROM tablename ORDER BY id ASC");

        $id = 'ID';
        $fullname = 'fullname';
        $password = 'password';
        $adres = 'adres';
        $telephone = 'telephone';
        $registration = 'registration';
        //while loop removed from here
        ?>
        <table width="100%" border="1">
            <thead>
                <tr>
                    <td>Klantennummer</td>
                    <td>Volledige naam</td>
                    <td>Email</td>
                    <td>Adres</td>
                    <td>Klant bewerken</td>
                    <td>Klant verwijderen</td>
                </tr>
            </thead>
            <tbody>
                <?php
                while ($rows = mysql_fetch_assoc($sql)) {
                    ?>
                    <tr>
                        <td><?php echo $rows['id']; ?></td>
                        <td><?php echo $rows['fullname']; ?></td>
                        <td><?php echo $rows['email']; ?></td>
                        <td><?php echo $rows['adres']; ?></td>
                        <td>missing !!</td>
                        <td>missing !!</td>
                    </tr>
                    <?php
                }
                ?>
            </tbody>
        </table>

Upvotes: 2

pradeep
pradeep

Reputation: 592

hai change the loop part

<?php
      while( $rows = mysql_fetch_assoc($sql) ){
      ?>
        <tr>
          <td><?php echo $rows['id']; ?></td>
          <td><?php echo $rows['fullname']; ?></td>
          <td><?php echo$rows['email']; ?></td>
          <td><?php echo $rows['adres']; ?></td>

        </tr>
  <?php
      }
    ?>`

Upvotes: -2

MaggsWeb
MaggsWeb

Reputation: 3027

Your issue is with lines like this:

<td>{$rows\['id'\]}</td>

Format them like this:

<td>".$rows['id']."</td>

Also, you have :

while ($rows = mysql_fetch_assoc($sql)){ 
}

that does not seem to do anything, that can be removed.

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94642

You have an extra while loop consuming all the results of your query, but not doing anything with the data, just delete the indicated code and it should at least do something

Also as @u_mulder says change the echo to remove unnecessay back slashes

<?php

require('db.php');

$sql = mysql_query("SELECT * FROM tablename ORDER BY id ASC");

$id = 'ID';
$fullname = 'fullname';
$password = 'password';
$adres = 'adres';
$telephone = 'telephone';
$registration = 'registration';

/*

 REMOVE THIS WHILE LOOP

while ($rows = mysql_fetch_assoc($sql)){ 

}

*/
?>
<table width="100%" border="1">
  <thead><tr>
    <td>Klantennummer</td>
    <td>Volledige naam</td>
    <td>Email</td>
    <td>Adres</td>
    <td>Klant bewerken</td>
    <td>Klant verwijderen</td>
  </tr>
  </thead>
  <tbody>
<?php
          while( $rows = mysql_fetch_assoc($sql) ){
            echo
            "<tr>
              <td>{$rows['id']}</td>
              <td>{$rows['fullname']}</td>
              <td>{$rows['email']}</td>
              <td>{$rows['adres']}</td>

            </tr>\n";
          }
        ?>
      </tbody>
    </table>

Upvotes: 1

u_mulder
u_mulder

Reputation: 54831

There's no need to add slashes before [].

Proper syntax should be:

<td>{$rows['id']}</td> // etc...

Upvotes: 0

Related Questions