PHP编写一个函数,可以接收一个表名,然后打印表的表头和记录在网页

<?php

    function show_table_info($table_name){

        $conn = MySQL_connect("localhost" , "root" , "root");
        if(!$conn){
            die('连接失败'.mysql_error());
        }

        mysql_select_db("test1",$conn);
        mysql_query("set names gbk");
//      $sql = "select * from user1";
        $sql = "desc user1";
        $res = mysql_query($sql , $conn);

        //获取结果集的行数、列数
        $rows = mysql_affected_rows($conn);  //参数是连接
        $cols = mysql_num_fields($res);      //参数是返回的资源
        echo "行:".$rows."<br>列:".$cols;

        //打印表头
        echo "<table border=1 cellspacing=0><tr>";
        for($i=0; $i<$cols; $i++){

            //取字段
            $mysql_name = mysql_field_name($res,$i);
            echo "<th>".$mysql_name."</th>";
        }
        echo "</tr>";

        while ($row = mysql_fetch_row($res)){
            echo "<tr>";
            for($i=0; $i<$cols; $i++){
                echo "<td>$row[$i]</td>";
            }
            echo "</tr>";
        }
        echo "</table>";

//         while($field_info = mysql_fetch_field($res)){
//             echo "<br>".$field_info->name;
//             /*打印结果:
//              * 
//                 id
//                 name
//                 password
//                 email
//                 age*/
//         }
        //var_dump($field_info);

    }

    show_table_info("user1");

网站标题:PHP编写一个函数,可以接收一个表名,然后打印表的表头和记录在网页
当前地址:https://www.cdcxhl.com/article46/gjjhhg.html

成都网站建设公司_创新互联,为您提供用户体验服务器托管企业建站云服务器微信公众号标签优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化