Haradzieniec
Haradzieniec

Reputation: 9338

MySQL: The easiest way to display all data from the table by getting TABLE_NAME olny (no column information) on the html page

MySQL db named "db2011" has several tables.

Using php variable $tablename (the tablename is correct, the table exists in the db), how to display the data from "db2011".$tablename on the html page?

Can it be done by performing just 1 query - to select all data by $tablename?

I think it can be done in 2 steps, but I'm asking any better solution (in case if this one is not good):

P.S. I know the data could be huge to be displayed on the html page. I will limit it by 100 rows.

Any better ways?

Thank you.

Upvotes: 1

Views: 1122

Answers (2)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40061

Why not just SELECT * FROM $tablename limit 100;?

You would get all the column names back in the result set. Unless you also need the column type on your webpage I would go with just that

Upvotes: 2

jjs9534
jjs9534

Reputation: 475

I am assuming you are doing this in PHP. It may not be elegant, but it gets it in one query. I think you want to display the table columns as well as the data in one query.

<?php

$sql = "SELECT * FROM $tablename";
$res = mysql_query($sql);
$firstpass = TRUE;
while($row = mysql_fetch_assoc($res)){
    if($firstpass){
        foreach($row as $key => $value) {
           //store all the column names here ($key)
           $firstpass = FALSE;
        }
    }
    //Collect all the column information down here.
}
?>

Upvotes: 3

Related Questions