Database Forum
Web Database Register Members List Calendar FAQ

 
Go Back   Database Forum > Database Management > PHP & MySQL
User Name
Password

Reply
 
Thread Tools Search this Thread Display Modes
Default Display MySQL table within a table
11-04-2009, 12:00 AM #1  

Explosivo22
Junior Member

 
Status: Offline
Posts: 2
Join Date: Nov 2009

I am still getting used to using mySQL and PHP. Have only been using for about a day and a half as of right now. I was just wondering if someone could help me take this code and make it so that the data I want it to display with be displayed inside of a table on my web page so it will have some sort of order to it and not just be in there. here is the code:
PHP Code:
<?PHP
mysql_connect
("localhost","username","password");
mysql_select_db("database");

$query = mysql_query("SELECT * FROM table");
while(
$row = mysql_fetch_array($query))
{
    
$first = $row["first"];
    
$last = $row["last"];
    
$email = $row["email"];
    echo
$first." ".$last." ".$email."<br>";
}

?>
I just need that information to display in a table and i have no idea how much information there is so i will need it to expand as more information is added to the database.

Thanks in advance.
Reply With Quote

Default
11-04-2009, 04:55 AM #2  

Kayla
Senior Member

 
Status: Offline
Posts: 302
Join Date: Mar 2005
Location: USA

Give this a try, replace text in bold:

<?
$usr = "USER";
$pwd = "PASS";
$db = "DBNAME";
$host = "localhost";

# connect to database
$cid = mysql_connect($host,$usr,$pwd);
mysql_select_db($db);
if (mysql_error()) { print "Database ERROR: " . mysql_error(); }

?>
<html>
<head>
<title>Results</title>
</head>
<body><center>
<?php

function drawtable($qr) {
// $qr = results of a mysql_query
$rows = mysql_num_rows($qr);
$toreturn = "<table style=\"float: center;\" border=\"2\">\n";
$toreturn .= "<tr>\n";
for ($i=0; $i<mysql_num_fields($qr); $i++) {
$toreturn .= "\t<th>".mysql_field_name($qr,$i)."</th>\n";
}
$toreturn .= "</tr>\n";
for ($i=0; $i<$rows; $i++) {
$row = mysql_fetch_row($qr);
$cols = sizeof($row);
$toreturn .= "<tr>\n";
for ($x=0; $x < $cols; $x++) {
if ($row[$x]==NULL){
$row[$x]='&nbsp;';
}
$toreturn .= "\t<td>$row[$x]</td>\n";
}
$toreturn .= "</tr>\n";
}
$toreturn .= '</table>';
return $toreturn;
}
$q = stripslashes($_POST['q']);
if (empty($q)) {
$q = 'select first, last, email from TABLE;';
}
mysql_select_db("DATABASENAME");
$result = mysql_query($q);

print "<b><font color=red>Your Query Result</font></b>\n";

echo drawtable($result);
echo '<table>
</table></center>
</body>
</html>';
Reply With Quote

Default
11-04-2009, 06:12 PM #3  

Explosivo22
Junior Member

 
Status: Offline
Posts: 2
Join Date: Nov 2009

Thanks. Works Just like I wanted it to. Thanks for all your help.
Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -5. The time now is 10:31 PM.

Powered by: vBulletin Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Database Forums

Help support our forum assistants and product development ->

go to top go to top