简体   繁体   中英

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>

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>

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>

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.

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

Proper syntax should be:

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

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
      }
    ?>`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM