• Home
  • Shell
    • Emacs
    • Perl
    • screen
    • sed
  • Ubuntu
    • VNC
  • Web Development
    • Javascript
    • Joomla
    • MySQL
    • osTicket
  • Windows
    • Gimp

Posts in category Uncategorized

Web Dev> Build HTML table from MySQL table using PHP

Aug23
2011
Written by Scott Rowley

Last night I was struggling for several hours trying to figure out how to get this working. After many googlings I finally found my answer and modified it to the following:
The $key in the following will be your column title (field) and the $value is the rows value. In the following I will be populating based off one particular row (in this case it has an id of 1).

print "<table border='1'>";
$sql = "SELECT * FROM table_name WHERE id=1";
$result = mysql_query($sql)or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
foreach($row as $key => $value) {
print "<tr><th>" .$key. "</th><td><input type='text' value='" .$value. "' /></td></tr>";
}
print "</table>";

MySQL table:
myql table

Resulting PHP/HTML table from code:
html table

Corrections? Questions? Comments?

Find an error?
Everything work out great for you?
Have some feedback?
Like to see something added to the article?

PLEASE leave us a comment after the article and let us know how we are doing, or if something needs corrected, improved or clarified.

Thank you!
- The Management

Advertisement

Sudo Bash
By Geeks - For Geeks

Back to Top