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):
Step1: Get column names by performing "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='db2011' AND TABLE_NAME='".$tablename."'";
Step2: Build and perform a query with SELECT + list of columns separated by comma FROM $tablename?
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.
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.
}
?>