返回顶部

收藏

php通用显示mysql库数据表方法

更多

php通用显示mysql库数据表方法:

<?php
include 'file/for/database/connection.php';
function displayTable($query = ""){
    $table = '';
    $sql = mysql_query($query);
    $table .= '<table>';
    $table .= '<tr>';
    while($field = mysql_fetch_field($sql)){
        $table .= '<th>'.$field->name.'</th>';
    }
    $table .= '</tr>';
    while($row = mysql_fetch_assoc($sql)){
        $table .= '<tr>';
        foreach($row as $key => $item){
            $table .= '<td style="padding-right:50px;">'.htmlentities($item).'</td>';
        }
        $table .= '</tr>';
    }
    $table .= '</table>';
    return $table;
}
echo displayTable("SELECT * FROM users"); // Displays all rows in the table
echo displayTable("SELECT username, email FROM users"); // displays only username and email in the table
?>

标签:php,mysql

收藏

0人收藏

支持

0

反对

0

发表评论