Blogger Information
Blog 16
fans 0
comment 0
visits 18053
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MySql数据库查询结果用表格输出PHP代码示例
忧郁之子的博客
Original
733 people have browsed it

在一般的网站中,我们会通常看到,很多数据库中表的数据在浏览器都是出现在表格中的,一开始让自己感到很神奇,但是仔细想想也不算太复杂,既然可以dql和dml的一般返回,以表格的方式返回应该也不成问题,但是,有一点说明的是,在客户端设计脚本去实现问题是不对的,即便可以实现起来也是非常复杂,所以,只能在服务器的方面去考虑,想想问题解决的方式就有了,即在返回的时候打印表格标签和对应属性和属性值,虽然说这种方式看起来不太合理,但是这也是最为有效的方法。具体的代码如下:

<?php
//在表格中显示表的数据,常用方式
    function ShowTable($table_name){
        $conn=mysql_connect("localhost","root","toor");
        if(!$conn){
            echo "连接失败";
        }
        mysql_select_db("test",$conn);
        mysql_query("set names utf8");
        $sql="select * from $table_name";
        $res=mysql_query($sql,$conn);
        $rows=mysql_affected_rows($conn);//获取行数
        $colums=mysql_num_fields($res);//获取列数
        echo "test数据库的"."$table_name"."表的所有用户数据如下:<br/>";
        echo "共计".$rows."行 ".$colums."列<br/>";
         
        echo "<table style='border-color: #efefef;' border='1px' cellpadding='5px' cellspacing='0px'><tr>";
        for($i=0; $i < $colums; $i++){
            $field_name=mysql_field_name($res,$i);
            echo "<th>$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>";
    }
    ShowTable("test1");
?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!