By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,742 Members | 773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,742 IT Pros & Developers. It's quick & easy.

How to display database table data in a html table.

P: n/a
Hi, I'm trying to display data from a mysql database in a HTML table
but for some reason my code isn't working. At the moment I have got it
to read and display the headers and the first row of the table and it
actually creates the remaining rows in the html table but it doesn't
put any data in them. This is my code so far:

<?php
$con = mysql_connect("localhost","REMOVED","REMOVED");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("db_03009319", $con);
?>

<?php $selectedTable=$_GET["selectedTable"] ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitled Document</title>
</head>
<body>
<?php echo $_GET["selectedTable"]; ?>
<table border=1>
<tr>
<?php
$headers = mysql_query("describe $selectedTable");

while($row = mysql_fetch_array($headers))
{
?>
<th><?php echo $row[0]; ?></th>
<?php
}
?>
</tr>
<?php
$headers = mysql_query("describe $selectedTable");

$data = mysql_query("select * from $selectedTable");
while($temp = mysql_fetch_array($data))
{
?>
<tr>
<?php
$i=0;
while(mysql_fetch_array($headers))
{
?>
<td><?php echo $temp[$i]; ?></td>
<?php
$i++;
}
?>
</tr>
<?php
}
?>
</table>
</body>
</html>

It displays various tables depending on which table name is passed to
it through the url so the number of columns cannot be a fixed number.
Any help would be appreciated.

Thanks,
Oliver

Jan 26 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
McGowan schreef:
Hi, I'm trying to display data from a mysql database in a HTML table
but for some reason my code isn't working. At the moment I have got it
to read and display the headers and the first row of the table and it
actually creates the remaining rows in the html table but it doesn't
put any data in them. This is my code so far:
I used this code once ... There are faster ways but I really dont use
tables anymore so I never rewrote the code.
// table top
echo '<table width="800">';

// first row contains table headers (not using th)
echo '<tr>';
foreach ($allheaders as $header)
{
echo '<td>'.$header.'</td>';
}
echo '</tr>';

// the data
foreach ($alldata as $data)
{
echo '<tr>';
foreach ($allheaders as $field)
{
// field is filled
if ($data[$field])
{
echo '<td>'.$data[$field].'</td>';
}
// empty field
else
{
echo '<td>&nbsp;</td>';
}
}
echo '</tr>';
}
echo '</table>';

--
Arjen
http://www.hondenpage.com
Jan 26 '07 #2

P: n/a
On Fri, 26 Jan 2007 12:19:58 -0500, McGowan <Bo******@gmail.comwrote:
<?php
$i=0;
while(mysql_fetch_array($headers))
{
?>
<td><?php echo $temp[$i]; ?></td>
<?php
$i++;
}
?>
Don't you actually want this while pointing at $data instead of $headers?
Or - get rid of this while entirely and move your $i=0 out one level.

Potentially the blind leading the blind here, as I'm relatively new to PHP
as well.

HTH
Jim

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jan 26 '07 #3

P: n/a
Rik
McGowan <Bo******@gmail.comwrote:
Hi, I'm trying to display data from a mysql database in a HTML table
but for some reason my code isn't working. At the moment I have got it
to read and display the headers and the first row of the table and it
actually creates the remaining rows in the html table but it doesn't
put any data in them. This is my code so far:
mysql_connect(); etc...
$result = mysql_query($query);
if($result){
print '<table>';
$row = mysql_fetch_assoc($result);
$heads = array_keys($row);
$columns = count($heads);
printf('<tr>'.str_repeat('<th>%s</th>',$columns).'</tr>',$heads);
printf('<tr>'.str_repeat('<td>%s</td>',$columns).'</tr>',$row);
while($row = mysql_fetch_assoc($result)){
printf('<tr>'.str_repeat('<td>%s</td>',$columns).'</tr>',$row);
}
print '</table>';
}
Optionally the printf()'s could be replaced by a nice
implode('</td><td>',$row);
--
Rik Wasmus
Jan 26 '07 #4

P: n/a
Thanks all. I've got it working now and I am less likely to hurt the
first person I see now. It was really starting to frustrate me. Coffee
probably doesn't help! :-D

Jan 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.