Displaying Table Row data - "hello, world" level

Hello, I’m taking an online mysql/PHP course through the big University extension where I live. We’re asked to create an html document that displays database table names (as a header) and to print the data contained in the table. Yep, it is probably the easiest thing… But not for someone who has been using Dreamweaver 4 for the last 4 years!
Here’s my coding (I’ve done my best to only present the important stuff here to save space):

[code]$result = mysql_query(“show tables”, $connection);

while($row = mysql_fetch_array($result, MYSQL_NUM)){
foreach ($row as $attribute)
print "{$attribute} - ";

$result = mysql_query(“select * from Athlete, Meet, Event, Results”, $connection);

while($row = mysql_fetch_array($result, MYSQL_NUM)){
foreach ($row as $attribute)[/code]

However, the class instructor asks me to streamline this code: “You don’t want the massive join statement of select * from table, table, table…Instead, do a while loop on the results from “Show tables” and then for each of those tables, do a select * from table. So you’ll have two loops and not a single table name in your code.”

How can you do a SELECT statement and not name the table?

Thanks for reading my first post (probably of many!)

You can use the results of your ‘SHOW TABLES’ query in your following queries, that way you don’t have to hardcode the tablename (which is what I assume your instructor means):

$resultset = mysql_query("SHOW TABLES");
while ($tablename = mysql_fetch_row($resultset)) {
  $tabledata = mysql_query("SELECT * FROM ".$tablename);
  while ($tablerow = mysql_fetch_row($tabledata)) {
    echo "<br>";

This is pseudocode and needs to be refined. Besides that, your instructor may not approve of using print_r() to display your table’s content.

Hello, and thank you for the reply!

Yes, my instructor wanted to see two while loops, instead of hardcoding. Your assumption was right on.
And while I do understand the coding you suggested, well… I thought I cleaned it up and ‘repurposed’ it enough to make it work, but it’s not quite…

[code]mysql_select_db(“garbed”, $connection);

$result = mysql_query(“show tables”, $connection);

while ($tablename = mysql_fetch_array($result)) {

$tabledata = mysql_query("SELECT * FROM ".$tablename, $connection);

while ($tablerow = mysql_fetch_array($tabledata)) {
echo “
It doesn’t like the bolded line of code there ( while ($tablerow = mysql_fetch_array($tabledata)) {
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource]

the code as written makes sense to me, but then again, you can count on two hands the number of days I’ve been typing php code. Help, please?
Thank you

Naturally I wouldn’t just hand you the solution to an educative exercise on a silver platter :wink: ) Echo() and print_r() statements should help you out.

Well thank you for your help so far, anyway. I believe there’s something with the $connection part of the script.

I’m in the University of Washington Extension Web Technology certificate program - it’s just four classes in length. HTML, CSS, Javascript, MySQL, PHP. I’m near the end of the 3rd class. The last course, my next one, is the capstone, where we will have to build a fully working website complete with … sheesh, everything it looks like. I can’t rely on DreamWeaver 4 forever.
I’ve used MS’ Expression Web program outfit, and it’s good in that all the styling is done in CSS, it’s basically Dreamweaver in automated CSS format.

I’ll probably be back! Thanks again. by the way, I went to a number of PHP help boards/sites, and phphelp.com has the best format and level of support / participation. That’s why I’m here, thanks for the good work!

Don in Seattle

Sponsor our Newsletter | Privacy Policy | Terms of Service