473,372 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

How to display database table data in a html table.

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
4 21262
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: JStrauss | last post by:
Hello, Can anybody give me some tips on the best and/or easiest way to display multiple lines in a field. The data is collected from a "textarea" field when the form is submitted, and I want to...
4
by: dmiller23462 | last post by:
Somebody take a look and give me any suggestions? My brain is nuked... Here's my deal....I have online submission forms on my intranet at work here....I am appending to an Access DB with the...
2
by: Joe | last post by:
Hi, I have an asp.net script that connects to MS Access database and displays data in a table. For some reason I am do not know how to display data say in ascending order of column1. I have...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
1
by: Objectifnet | last post by:
What I really want to do is to be able to link two pages together using an ID, The table involved displays an image stored on the File Server that has the image details stored in the Database called....
1
by: Dan Somdahl | last post by:
Hi, I am new to ASP but have what should be a fairly simple task that I can't figure out. I need to parse a string from a single, semi-colon delimited, 60 character field (el_text) in a recordset...
7
by: nma | last post by:
Hi I'm trying to display data in a table list format but error message like this appear. What is wrong with the code? "Parse error: syntax error, unexpected '<' in C:\htdocs\...\MB2movieinfo.php...
3
by: phil67b | last post by:
Hello everybody, I have a page rech.php where I'm doing a multi-criteria research Ex. choose your car model, choose your country. After validation of my form, on the same page, the lines will be...
1
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.