PHP获取MySQL数据库中的整张表的数据

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取数据表中的数据</title>
</head>
<body>
<?php
    $table= 'grades';
    $database= 'sel';
    $link= @mysqli_connect('localhost','root','',$database) or die('错误:'.mysqli_connect_error());
    mysqli_set_charset($link,'utf8');
    $rs= mysqli_query($link,'select * from '.$table);   
    $list= mysqli_fetch_all($rs,MYSQLI_ASSOC);  
?>
    <table>
        <tr>
            <?php foreach($list as $key=>$value): ?>
                <th><?php echo array_keys($value)[$key] ?></th>
            <?php endforeach;?>
        </tr>
        <?php foreach($list as $rows):?>
        <tr>
            <?php for($i=0; $i<count($list); $i++):?>
                <td><?php echo $rows[array_keys($rows)[$i]]?></td>
            <?php endfor;?>
        </tr>
        <?php endforeach;?>
    </table>
</body>
</html>
原文地址:https://www.cnblogs.com/SharkJiao/p/14087606.html