MySQL database table to HTML table

后端 未结 2 613
一生所求
一生所求 2021-01-28 06:17

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 an

2条回答
  •  误落风尘
    2021-01-28 06:54

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

    php:

    function mysql_fetch_all($res) {
        $result = array();
        while ($row = mysql_fetch_row($res)) {
            $result[] = $row;
        }
        return $result;
    }
    
    function getColumnsAndData($table) {
        $table = mysql_real_escape_string($table);
        $q1 = mysql_query("show columns from $table");
        $q2 = mysql_query("select * from $table");
        return array(mysql_fetch_all($q1), mysql_fetch_all($q2));
    }
    
    list($columns, $data) = getColumnsAndData($table);
    ?>
    

    html

     0): ?>
                
    No data to display

提交回复
热议问题