zoukankan      html  css  js  c++  java
  • 通过表名显示数据库中该表的表头和内容(mysql扩展库操作)

    • 编写一个函数,接收一个表名,然后把表的表头和内容显示在网页
     1 <?php
     2     function readTab($tableName){
     3         $conn=mysql_connect("localhost","root","root");
     4         if(!$conn){
     5             die("连接失败".mysql_error());
     6         }
     7         mysql_select_db("test",$conn);
     8         mysql_query("set names utf8",$conn);
     9         $sql="select * from $tableName";
    10         $res=mysql_query($sql,$conn);
    11         //读取结果集中的行数
    12         $rows=mysql_affected_rows($conn);
    13         //读取结果集中的列数,mysql_num_fields()返回结果集中字段的数目
    14         $colums=mysql_num_fields($res);
    15         //显示表头
    16         echo "<table border=1 cellspacing=0 cellpadding=3px><tr>";
    17         for($i=0;$i<$colums;$i++){
    18             //mysql_field_name()取出结果集中指定字段的字段名
    19             $fieldName=mysql_field_name($res,$i);
    20             echo "<th>$fieldName</th>";
    21         }
    22         echo "</tr>";
    23         //显示表格内容
    24         while($row=mysql_fetch_row($res)){
    25             echo "<tr>";
    26             for($j=0;$j<$colums;$j++){
    27                 echo "<td>$row[$j]</td>";
    28             }
    29             echo "</tr>";
    30         }
    31         echo "</table>";
    32         mysql_close($conn);
    33     }
    34     //调用函数readTab
    35     readTab("user1");
    36 ?>

    结果显示如下:

  • 相关阅读:
    在Exchange 2013中重置用户密码
    在exchange邮箱服务器上启用反垃圾邮件功能
    EMC队列 发件人为空 From Address: <>
    zabbix删除历史记录
    ESXi 6.7 CVE-2018-3646警告的处理
    CentOS安装nmap端口查看工具
    webpack学习
    vscode 点滴
    chrome点滴
    前端资料汇总
  • 原文地址:https://www.cnblogs.com/seaBiscuit0922/p/5917389.html
Copyright © 2011-2022 走看看