37)PHP,获取数据库值并在html中显示(晋级2)

下面的是上一个的改进版,我知道为啥我的那个有问题了,因为我的__construct()这个函数的里面的那个变量名字搞错了,哎,这是经常犯得毛病,傻了吧唧,气死我了。

之前的那个变量的代码样子:

 1 class db
 2     {
 3         public $host = "localhost";//定义默认连接方式
 4         public $User= "root";//定义默认用户名
 5         public $Pwd= "root";//定义默认的密码
 6         public $Dbname = "thkphp5";//定义默认的数据库名
 7         public $my_sql;
 8         public $link;
 9         public $result;
10         public function __construct($host,$user,$pwd,$dbname,$sql) {
11         $this->host=$host;
12         $this->zhang=$user;
13         $this->mi=$pwd;
14         $this->dbname=$dbname;
15         $this->my_sql=$sql;
16       $this->link=  $this->connect();
17       $this->result=  $this->Query($this->my_sql);
18         }
19     

看我的加粗部分,你看到了嘛,明明对应错了,所以啊,怪不得之后的代码不买账,你说你变量的名字都对应错了,人家再是小三语言,也会报错啊,所以啊,认真点

改进后的那段代码:

 1 class db
 2     {
 3         public $host ;//= "localhost";//定义默认连接方式
 4         public $User;//= "root";//定义默认用户名
 5         public $Pwd;//= "root";//定义默认的密码
 6         public $Dbname ;//= "thkphp5";//定义默认的数据库名
 7         public $my_sql;
 8         public $link;
 9         public $result;
10         public function __construct($host,$user,$pwd,$dbname,$sql) {
11         $this->host=$host;
12         $this->User=$user;
13         $this->Pwd=$pwd;
14         $this->Dbname=$dbname;
15         $this->my_sql=$sql;
16       $this->link=  $this->connect();
17       $this->result=  $this->Query($this->my_sql);
18         }

现在看到区别了吧,现在编译也对了,没有错误了

总的代码展示:

 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;
 9         public $link;
10         public $result;
11         public function __construct($host,$user,$pwd,$dbname,$sql) {
12         $this->host=$host;
13         $this->User=$user;
14         $this->Pwd=$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             $Link= mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname);
40             return $Link;
41         }
42         
43     }
44     $sql='select * from zixun;';
45    $shujuku=new db("localhost","root","root","thkphp5",$sql);
46 
47 
48   include './login.html';
49 //var_dump($shujuku->result);
50 
51 ?>

然后我的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($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>

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

是那个b.php和login.html,不是BBB.php,那个图是上一个的,我没有重新弄,我太懒了。

然后就是结果展示:

 数据库代码展示:

 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 11:00  小油菜1  阅读(619)  评论(0编辑  收藏  举报