首先展示我的html代码和php文件的位置关系:
然后我的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; 9 public $link; 10 public $result; 11 /* 12 * 构造函数 13 * 主机名,使用者,使用者密码,数据库的名字,查询语句 14 */ 15 public function __construct($config) { 16 $this->host=$config['host']; 17 $this->User=$config['user']; 18 $this->Pwd=$config['pwd']; 19 $this->Dbname=$config['dbname']; 20 $this->my_sql=$config['sql']; 21 $this->link= $this->connect(); 22 $this->result= $this->Query($this->my_sql); 23 } 24 25 //成员方法 是用来执行sql语句的方法 26 /* 27 * 数据库查询函数 28 * $sql string 是你的查询语句 29 */ 30 public function Query($sql) 31 //两个参数:sql语句,判断返回1查询或是增删改的返回 32 { 33 $db = $this->connect(); 34 $r = $db->query($sql); 35 if (isset($r)) { 36 return $r->fetch_all();//查询语句,返回数组.执行sql的返回方式是all,也可以换成row 37 } else { 38 return "数据库查询失败!"; 39 } 40 41 42 } 43 /* 44 * 数据库连接函数 45 */ 46 public function connect(){ 47 $Link= mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname); 48 return $Link; 49 } 50 51 } 52 $sql='select * from zixun;'; 53 $config=array('host'=>"localhost",'user'=>"root",'pwd'=>"root",'dbname'=>"thkphp5",'sql'=>$sql); 54 $shujuku=new db($config); 55 56 57 include './login.html'; 58 //var_dump($shujuku->result); 59 60 ?>
然后我的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>
然后我的结果展示:
改进的部分是:就是把之前的在__construct()函数中传值,一个一个的对应传入,变成了,我的数组$config,这样传入数据,其实,我还是可以在弄一个配置文件的,然后载入配置文件,将结果输入到我的主php文件的__construct()函数中。
数据库代码展示:
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,'草稿');