Home > php教程 > php手册 > body text

MySql数据库查询结果用表格输出PHP代码示例

WBOY
Release: 2016-06-13 09:10:24
Original
954 people have browsed it

MySql数据库查询结果用表格输出PHP代码示例

 这篇文章主要介绍了MySql数据库查询结果用表格输出PHP代码示例,本文直接给出代码示例,需要的朋友可以参考下

 

 

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

//在表格中显示表的数据,常用方式

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"."表的所有用户数据如下:
";

echo "共计".$rows."行 ".$colums."列
";

 

echo "

";

for($i=0; $i

$field_name=mysql_field_name($res,$i);

echo "

";

}

echo "

";

while($row=mysql_fetch_row($res)){

echo "

";

for($i=0; $i

echo "

";

}

echo "

";

}

echo "

$field_name
$row[$i]
";

}

ShowTable("test1"); 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!