Home > Backend Development > PHP Tutorial > MySql database query results output in table PHP code example_PHP tutorial

MySql database query results output in table PHP code example_PHP tutorial

WBOY
Release: 2016-07-13 10:01:21
Original
863 people have browsed it

MySql database query results using table output PHP code example

This article mainly introduces MySql database query results using table output PHP code example, this article directly gives the code example, need Friends can refer to it

In general websites, we usually see that the data of many database tables appear in tables in the browser. It feels amazing at first, but it is not too complicated if you think about it carefully. Since dql and dml can be returned in general, it should not be a problem to return in the form of a table. However, one thing to note is that it is wrong to design a script on the client to implement the problem. Even if it can be implemented, it is very complicated, so just You can think about it from the perspective of the server and think about the way to solve the problem, that is, print the table label and the corresponding attributes and attribute values ​​when returning. Although this method seems unreasonable, it is also the most effective. method. The specific code is as follows:

?

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 < $colums; $i ){

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

echo "

";

}

echo "

";

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

echo "

";

for($i=0; $i<$colums; $i ){

echo "

";

}

echo "

";

}

echo "

$field_name
$row[$i]
";

}

ShowTable("test1"); 

1 2 3 4

56 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
"; echo "Total".$rows."rows ".$colums."Columns
"; echo ""; for($i=0; $i < $colums; $i ){<🎜> <🎜>$field_name=mysql_field_name($res,$i);<🎜> <🎜>echo ""; } echo ""; while($row=mysql_fetch_row($res)){ echo ""; for($i=0; $i<$colums; $i ){<🎜> <🎜>echo ""; } echo ""; } echo "
$field_name
$row[$i]
"; } ShowTable("test1");
http://www.bkjia.com/PHPjc/971949.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/971949.htmlTechArticleMySql database query results using a table to output PHP code example This article mainly introduces MySql database query results using a table to output PHP Code examples, this article directly gives code examples, friends in need...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template