本文實例講述了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");
?>
希望本文所述對大家的php程序設計有所幫助。