1

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>
1
  • 2
    Did you even verify you can connect to your database? - What's the contents of db.php? Commented Aug 16, 2015 at 12:41

5 Answers 5

2

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

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.

Comments

0

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

Proper syntax should be:

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

Comments

-2

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

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.