36)PHP,搜寻数据库信息在html中显示(晋级1)

首先是数据库的样子展示:

然后就是我的PHP主文件了:

 1 <?php
 2     class db
 3     {
 4         public $host="localhost" ;//这个是主机的地址
 5         public $User="root";//数据库的使用者
 6         public $Pwd="root" ;//数据库使用者的密码
 7         public $Dbname="thkphp5";//数据库的名字
 8         public $my_sql;//这个是你搜寻的mysql语句
 9         public $link;//这个是连接结果
10         public $result;//这个是搜寻的结果
11         public function __construct($host,$user,$pwd,$dbname,$sql) {
12             $this->host=$host;
13             $this->zhang=$user;
14             $this->mi=$pwd;
15             $this->dbname=$dbname;
16             $this->my_sql=$sql;
17             $this->link=  $this->connect();
18             $this->result=  $this->Query($this->my_sql);
19         }
20         
21         //成员方法   是用来执行sql语句的方法
22         public function Query($sql,$type=1)
23             //两个参数:sql语句,判断返回1查询或是增删改的返回
24         {
25             //造一个连接对象,参数是上面的那四个
26             //            $db = new mysqli($this->host,$this->zhang,$this->mi,$this->dbname);
27             $db=$this->connect();
28             $r = $db->query($sql);
29             if($type == "1")
30             {
31                 return $r->fetch_all();//查询语句,返回数组.执行sql的返回方式是all,也可以换成row
32             }
33             else
34             {
35                 return $r;
36             }
37         }
38         public function connect(){
39             return mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname);
40         }
41         
42     }
43     $sql='select * from zixun;';
44     $shujuku=new db('localhost','root','root','thkphp5',$sql);
45     
46     //var_dump($shujuku->result);//为啥有这个呢,因为我要看一下我的result的结果是什么样,不然会在我的html中报错
47     include './login.html';
48     
49 
50 ?>

然后展示我的htnl代码:

 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($shujuku->result as $row) : ?>
15     <tr>
16         <td><?php echo $row[0];?></td>
17         <td><?php echo $row[1];?></td>
18         <td><?php echo $row[2];?></td>
19         <td><?php echo $row[3];?></td>
20         <td><?php echo $row[4];?></td>
21         <td><?php echo $row[5];?></td>
22         <td><?php echo $row[6];?></td>
23     </tr>
24     <?php endForeach;?>
25 </table>
26 </body>
27 </html>

我的html文件和php文件的位置关系:

然后我的想法总结:     首先看一下我用var_dump($result)的结果是什么样的:

这个代码有一个问题,就是我的那个connect函数是用的我之前的默认值,就是那几个参数是默认值,不是我后来传进去的值,因为我发现后来传进去的值根本没事别,除了那个$sql,哎,有问题,就得改啊,

 

看到了吗,这个$result是一个二维数组,所以,我在那个login.html中用的就是$row[0],因为这个$row是一个一维数组

 数据库代码展示:

 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,'草稿');

 

posted @ 2017-08-14 10:59  小油菜1  阅读(461)  评论(0编辑  收藏  举报