龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

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

时间:2014-06-26 02:05来源:网络整理 作者:网络 点击:
分享到:
php通用显示mysql库数据表方法 php通用显示mysql库数据表方法:```{.php}lt;?phpinclude #39;file/for/database/connection.php#39;;function displayTable($query = quot;quot;){ $table = #39;#39;; $sql = mysql_query($query);

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
?>
精彩图集

赞助商链接