34)PHP,PHP从数据库读取数据并在html中显示
首先是我的数据库截图:
然后展示我的php文件:
1 b.php文件: 2 <?php 3 4 5 $link= mysqli_connect('localhost','root','root'); 6 // mysqli_character_set_name(); 7 mysqli_query($link,"set names utf8"); 8 mysqli_select_db($link,'thkphp5'); 9 $sql='select * from zixun'; 10 $result=mysqli_query($link,$sql); 11 $records=array(); 12 while( $record = mysqli_fetch_array($result )){ 13 //mysql_fetch_array()函数的作用类似foreach对数组的作用:遍历(结果集) 14 //它每次去的结果集的“一行数据”,并“装入”到数组 $record 中 15 //该数组的下标就是该select的字段名,值就是对应行的数据值 16 $records[]=$record; 17 } 18 // var_dump($records); 19 require 'login.html'; 20 //为啥能那个顺利的展示,那个require 'login.html'; 就是把login.html页面照搬到这个b.php文件中
21 ?>
然后那个html文件:
1 <!-- 模板文件,利用HTML代码展示数据 --> 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title>比赛列表</title> 7 </head> 8 <body> 9 10 <table> 11 <tr> 12 <th>ZX_id</th><th>ZX_name</th><th>ZX_fenlei</th><th>ZX_zuozhe</th><th>更新时间</th><th>浏览次数</th><th>发布状态</th> 13 </tr> 14 <?php foreach($records as $row) : ?> 15 <tr> 16 <td><?php echo $row['ZX_id'];?></td> 17 <td><?php echo $row['ZX_name'];?></td> 18 <td><?php echo $row['ZX_fenlei'];?></td> 19 <td><?php echo $row['ZX_zuozhe'];?></td> 20 <td><?php echo $row['gengxin_time'];?></td> 21 <td><?php echo $row['liulan_cishu'];?></td> 22 <td><?php echo $row['fabu_zhuangtai'];?></td> 23 </tr> 24 <?php endForeach;?> 25 </table> 26 </body> 27 </html>
最后的结果展示:
数据库代码展示:
1 CREATE DATABASE `thkphp5` ; 2 use thkphp5 ; 3 create table zixun( 4 ZX_id int not null auto_increment primary key comment '咨询ID号', 5 ZX_name VARCHAR(80) NOT NULL COMMENT '咨询标题', 6 ZX_fenlei varchar(80) not null comment '资讯分类', 7 ZX_zuozhe varchar(80) not null comment '资讯作者', 8 gengxin_time DATETIME NOT NULL DEFAULT '2016-01-01 01:01:01' COMMENT '更新时间', 9 liulan_cishu int NOT NULL COMMENT '浏览次数', 10 fabu_zhuangtai VARCHAR(50) NOT NULL COMMENT '发布状态' 11 )engine=MyISAM charset=utf8; 12 INSERT into zixun(ZX_id, ZX_name, ZX_fenlei, ZX_zuozhe, gengxin_time, liulan_cishu, fabu_zhuangtai) values(10001, 'PHP', '理论', '王超', '2017-08-07 11:58:01', 100, '草稿'); 13 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10002,'C语言','理论','王超','2017-08-07 11:58:01',100,'草稿'); 14 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10003,'JAVA语言','理论','王超','2017-08-07 11:58:01',100,'草稿'); 15 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10004,'Mysql语言','理论','王超','2017-08-07 11:58:01',100,'草稿'); 16 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10005,'html','理论','王超','2017-08-07 11:58:01',100,'草稿'); 17 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10006,'spring','理论','王超','2017-08-07 11:58:01',100,'草稿'); 18 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10007,'scence','理论','王超','2017-08-07 11:58:01',100,'草稿'); 19 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10008,'computer','理论','王超','2017-08-07 11:58:01',100,'草稿'); 20 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10009,'math','理论','王超','2017-08-07 11:58:01',100,'草稿'); 21 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(100010,'english','理论','王超','2017-08-07 11:58:01',100,'草稿'); 22 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10011,'word','理论','王超','2017-08-07 11:58:01',100,'草稿'); 23 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10012,'jsp','理论','王超','2017-08-07 11:58:01',100,'草稿'); 24 INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10013,'CSS','理论','王超','2017-08-07 11:58:01',100,'草稿');