eraxian Posted March 22, 2008 Share Posted March 22, 2008 Hello guys, I'm new to PHP and web development in general so go easy on me. Here's the sitrep. I created a database on a mysql server, it has 1 field. I want to create an html table dynamically when users click on a link, and display the data from the mysql database. I found several scripts online for doing this but none have worked. This is what I have so far: <!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> <?php $con = mysql_connect("myservername","myusername","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bands", $con); $result = mysql_query("SELECT * FROM `list` WHERE name LIKE \'A%\' ORDER BY name"); echo "<table border='1'> <tr> <th>Band Name</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> <body> </body> </html> AND this is the actual output I get when I open this page, "Band Name "; while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['name'] . ""; echo ""; } echo ""; mysql_close($con); ?>" So obviously somewhere I've made a grievous error, please help! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/97419-creating-dynamic-html-table/ Share on other sites More sharing options...
Barand Posted March 22, 2008 Share Posted March 22, 2008 try this <?php $sql = "SELECT * FROM `list` WHERE name LIKE \'A%\' ORDER BY name"; echo table2Table ($sql); function table2Table($query) { $result = mysql_query($query); $str = "<TABLE border='1' cellpadding='4'>\n"; // column headings $str .= "<tr>\n"; while ($fld = mysql_fetch_field ($result)) { $str .= "<th>{$fld->name}</th>\n"; } $str .= "</tr>\n"; // list data while ($row = mysql_fetch_row($result)) { $str .= "<tr>\n"; foreach ($row as $field) { $str .= "<td>$field</td>\n"; } $str .= "</tr>\n"; } $str .= "</TABLE>\n"; return $str; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97419-creating-dynamic-html-table/#findComment-498478 Share on other sites More sharing options...
eraxian Posted March 23, 2008 Author Share Posted March 23, 2008 still having the same type of issue, this is what I get when i inserted the code you posted, \n"; // column headings $str .= "\n"; while ($fld = mysql_fetch_field ($result)) { $str .= "{$fld->name}\n"; } $str .= "\n"; // list data while ($row = mysql_fetch_row($result)) { $str .= "\n"; foreach ($row as $field) { $str .= "$field\n"; } $str .= "\n"; } $str .= "\n"; return $str; } ?> Why is it not recognizing the rest of the code as part of the php script??? i'm baffled, plz help! Quote Link to comment https://forums.phpfreaks.com/topic/97419-creating-dynamic-html-table/#findComment-498590 Share on other sites More sharing options...
sqlnoob Posted March 23, 2008 Share Posted March 23, 2008 you sure that you stored as a php file and that php is installed where you stored the php file? also can you show me the table where you get the info from Quote Link to comment https://forums.phpfreaks.com/topic/97419-creating-dynamic-html-table/#findComment-498791 Share on other sites More sharing options...
eraxian Posted March 25, 2008 Author Share Posted March 25, 2008 you were right 'sqlnoob', i forgot to save it as a .php file, now i feel like an idiot, lol. it works now, thanks Quote Link to comment https://forums.phpfreaks.com/topic/97419-creating-dynamic-html-table/#findComment-500373 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.