<?php
function show_tab_info($table_name){
$conn=mysql_connect("localhost","root","");
if(!$conn){
die("连接失败".mysql_error());
}
mysql_select_db("test",$conn);
mysql_query("set names GBK");
$sql="select * from $table_name";
$res=mysql_query($sql,$conn);
//要知道总有的多少行 多少列
$rows=mysql_affected_rows($conn);//获取多少行
$colums=mysql_num_fields($res);//获取多少列
echo "<table border=1><tr>";
//表头
for($i=0;$i<$colums;$i++){
$field_name=mysql_field_name($res,$i);
echo "<th width=40>$field_name</th>";
}
echo "</tr>";
//输出数据
while($row=mysql_fetch_row($res)){//获取每行数据
echo "<tr>";
for($i=0;$i<$colums;$i++){
echo "<td>$row[$i]</td>";
}
echo "</tr>";
}
echo "</table>";
while($field_info=mysql_fetch_field($res)){
echo "<br/>".$field_info->name;
}
}
show_tab_info("user1");
?>
获取数据库数据并输出到表中(定义函数)
最新推荐文章于 2021-01-27 09:55:22 发布