Matt Matt - 1 year ago 76
HTML Question

Putting SQL data into HTML table

I'm trying to get the data from my mySQL database and put them into a HTML table.
After searching a lot on the internet, but I coudn't find code that worked for me.

Currently, I have this code

<!DOCTYPE html>
<html xmlns="">
$db_select = mysql_select_db($dbname,$db);
if (!db_select) {
die("Database selection also failed miserably: " . mysql_error());
$results = mysql_query("SELECT NaamFuif, GemeenteFuif, DatumFuif FROM tblfuiven");
while($row = mysql_fetch_array($results)) {
<td><?php echo $row['NaamFuif']?></td>
<td><?php echo $row['GemeenteFuif']?></td>
<td><?php echo &row['DatumFuif']?></td>


The only thing that I get is the first row of my table (Naam-Gemeente-Datum).
Am I doing something wrong or did I forgot something?

Answer Source

First of all, the most important thing to keep in mind is:

You are using deprecated and unsecure code

The mysql_ functions are strongly discouraged, for various reasons:

  • Are deprecated and will be removed in future versions of PHP,
  • Are insecure leading to possible SQL injections,
  • Lack many features present in more current versions of PHP

See the linked question for much more in-depth explanations.

Now, to the code itself:

You are not using mysql_connect to connect to the server

You should use mysql_connect to specify the server, the username and the password that will be used to access the data in the database. From your code, it seems that it was supposed to be present, because there's a $db variable used in the mysql_connect function, but not properly initialized nor used again anywhere else.

You should use mysql_connect in a way similar to this:

$db = mysql_connect('localhost', $user, '$password');
if (!$db) {
    die('Not connected : ' . mysql_error());

(Don't forget to set your username and password!)

You are using mysql_select_db twice in a row:

    $db_select = mysql_select_db($dbname,$db);
        if (!db_select) {
            die("Database selection also failed miserably: " . mysql_error());

followed by


  1. Note the $dbname and $db variables, you don't have them on your code, this function won't work like this.
  2. The second mysql_select_db overwrites the first, but you don't specify a server connection to be used.

You should use the first version, but you should use mysql_connect before it.

You have typos in your code

  • if (!db_select) { should be if (!$db_select) {
  • echo &row['DatumFuif'] should be echo $row['DatumFuif']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download