php+mysqli实现将数据库中一张表信息打印到表格里的方法
程序员文章站
2022-05-06 21:26:46
本文实例讲述了php+mysqli实现将数据库中一张表信息(包括表头)打印到表格里的方法。分享给大家供大家参考。具体如下:
这段代码将就看吧。需要学习基础知识。代码如下:...
本文实例讲述了php+mysqli实现将数据库中一张表信息(包括表头)打印到表格里的方法。分享给大家供大家参考。具体如下:
这段代码将就看吧。需要学习基础知识。代码如下:
复制代码 代码如下:
<?php
$mysqli = new mysqli("localhost","root","123456","liuyan");
if(!$mysqli){
die($mysqli->error);
}
function showtable($mysqli,$table_name){
$sql = "select * from $table_name";
$res = $mysqli->query($sql);
//获取返回总行数和列数
echo "共有:".$res->num_rows."行;共有:".$res->field_count."列。";
//获取表头---从$res取出
echo "<table border=1><tr>";
while($field=$res->fetch_field()){
echo "<th>{$field->name}</th>";
}
echo "</tr>";
//循环取出数据
while($row=$res->fetch_row()){
echo "<tr>";
foreach($row as $value){
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>";
$res->free();
}
showtable($mysqli,"news");
?>
$mysqli = new mysqli("localhost","root","123456","liuyan");
if(!$mysqli){
die($mysqli->error);
}
function showtable($mysqli,$table_name){
$sql = "select * from $table_name";
$res = $mysqli->query($sql);
//获取返回总行数和列数
echo "共有:".$res->num_rows."行;共有:".$res->field_count."列。";
//获取表头---从$res取出
echo "<table border=1><tr>";
while($field=$res->fetch_field()){
echo "<th>{$field->name}</th>";
}
echo "</tr>";
//循环取出数据
while($row=$res->fetch_row()){
echo "<tr>";
foreach($row as $value){
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>";
$res->free();
}
showtable($mysqli,"news");
?>
希望本文所述对大家的php程序设计有所帮助。