韩顺平php mysql扩展库函数的应用案例

请编写一个函数,可以接受一个表名,然后把表的头和记录显示在网页

<?php
function show_tab_info($table_name){
 $conn=mysql_connect("localhost","root","root");
 if(!$conn){
	 die(mysql_error());
 
 }
mysql_select_db("test",$conn);
mysql_query("set names uft8");
$sql="select*from $table_name";
//$sql="desc $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);//mysql_field_name函数取得结果中指定字段的字段名。
   echo "<th>$field_name</th>";
 }
echo "</tr>";

while($row=mysql_fetch_row($res)){//mysql_fetch_row 函数从结果集中取得一行作为数字数组。
  echo "<tr>";
    for($i=0;$i<$colums;$i++){
      echo "<td>$row[$i]</td>";
	 }
 
  echo "</tr>";

}
echo "</table>";

/*while($field_info=mysql_fetch_field($res)){//mysql_fetch_field 函数从结果集中取得列信息并作为对象返回
	echo "<br/>".$field_info->name;
}*/

mysql_free_result($res);//获得资源后要及时释放资源
mysql_close($conn);

}

show_tab_info(user1);

}




在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43345480/article/details/89556940