Skip to content Skip to sidebar Skip to footer

Mysql Database Table To Html Table

Here the value of $table will be passed from another file and it will be a table name in a database. With that table name ($table) am trying to fetch its column names and all the v

Solution 1:

Yes only columns will be printing because you are printing $rs[0];?> where $rs holds object for mysql_query qq and it holds your column query only not

"select * from $table"

this.

Why dont you try displaying columns as a single row in html table and then try displaying data from other php set with seperate query structure . Hope this way it helps.

Solution 2:

I coudn't fix your code because it was getting ugly, so i remade it:

php:

functionmysql_fetch_all($res) {
    $result = array();
    while ($row = mysql_fetch_row($res)) {
        $result[] = $row;
    }
    return$result;
}

functiongetColumnsAndData($table) {
    $table = mysql_real_escape_string($table);
    $q1 = mysql_query("show columns from $table");
    $q2 = mysql_query("select * from $table");
    returnarray(mysql_fetch_all($q1), mysql_fetch_all($q2));
}

list($columns, $data) = getColumnsAndData($table);
?>

html

<tablecellpadding="0"cellspacing="0"border="0"width="100%"class="display"rel="datatable"><thead><tr><?phpforeach ($columnsas$column): ?><td><?phpecho$column[0] . ' ' . $column[1] ?></td><?phpendforeach; ?><tr></thead><tbody><?phpif (count($data) > 0): ?><?phpforeach ($dataas$row): ?><tr><?phpforeach ($rowas$value): ?><td><?phpecho$value?></td><?phpendforeach; ?><tr><?phpendforeach; ?><?phpelse: ?><tr><td>No data to display</td></tr><?phpendif; ?></tbody></table>

Post a Comment for "Mysql Database Table To Html Table"