获取数据库数据并输出到表中(定义函数)

<?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");

?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值