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="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<table>
<thead>
<tr>
<td>Naam</td>
<td>Gemeente</td>
<td>Datum</td>
</tr>
</thead>
<tbody>
<?php
$db_select = mysql_select_db($dbname,$db);
if (!db_select) {
die("Database selection also failed miserably: " . mysql_error());
}
mysql_select_db("databaseiheko");
$results = mysql_query("SELECT NaamFuif, GemeenteFuif, DatumFuif FROM tblfuiven");
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['NaamFuif']?></td>
<td><?php echo $row['GemeenteFuif']?></td>
<td><?php echo &row['DatumFuif']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
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?
First of all, the most important thing to keep in mind is:
The mysql_ functions are strongly discouraged, for various reasons:
See the linked question for much more in-depth explanations.
Now, to the code itself:
mysql_connect
to connect to the serverYou 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!)
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
mysql_select_db("databaseiheko");
$dbname
and $db
variables, you don't have them on your code, this function won't work like this.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.
if (!db_select) {
should be if (!$db_select) {
echo &row['DatumFuif']
should be echo $row['DatumFuif']
mysql_ functions are deprecated, but if you want to use them, i suggest these corrections:
You can correct your code this way: the mysql connect is needed:
<?php
//connect to your database
mysql_connect("serverIpAddress","userName","password");
//specify database
mysql_select_db("yourDatabaseName") or die;
//Build SQL Query
$query = "select * from tblfuiven";
$queryResult=mysql_query($query);
$numrows=mysql_num_rows($queryResult);
numrows will contain the number of found records in the db. add an echo for the number of rows and let us know if the number of rows is still one. Then use mysql_fetch_assoc to get rows:
while($row = mysql_fetch_assoc($queryResult)) {
?>
<tr>
<td><?php echo $row['NaamFuif']?></td>
<td><?php echo $row['GemeenteFuif']?></td>
<td><?php echo &row['DatumFuif']?></td>
</tr>
<?php
}
?>
EDIT: You can test the code and let us know the number of rows that you obtain, using this code(write your real user name and password and db name:
<?php
mysql_connect("localhost","root","root");
mysql_select_db("databaseName") or die;
$results = mysql_query("SELECT * FROM tblfuiven");
$numrows=mysql_num_rows($queryResult);
while($row = mysql_fetch_assoc($queryResult)) {
?>
<tr>
<td><?php echo $numrows ?></td>
<td><?php echo $row['NaamFuif']?></td>
<td><?php echo $row['GemeenteFuif']?></td>
<td><?php echo $row['DatumFuif']?></td>
</tr>
<?php
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With