38)PHP,获取数据库数据并在html中显示(晋级5)
还有一个加了单例模式的,在第52个。
首先是我的文件关系:
我的主php文件是index.php,我的配置文件php是BBB.php 我的数据库操作文件是 b.php 我的html文件是login.html
我的index.php代码展示:
1 <?php 2 $sql='select * from zixun;'; 3 $config=include './BBB.php'; 4 include './b.php'; 5 6 $shujuku=new db($config); 7 include './login.html';
我的BBB.php代码展示:
1 <?php 2 return $config=array( 3 'host'=>"localhost", 4 'user'=>"root", 5 'pwd'=>"root", 6 'dbname'=>"thkphp5", 7 'sql'=>$sql);
我的b.php代码展示:
1
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 // protected static $_dbh = null; //静态属性,所有数据库实例共用,避免重复连接数据库,这个是学来的,看的别人的代码,觉得不错,摘过来的 12 /* 13 * 构造函数 14 * 主机名,使用者,使用者密码,数据库的名字,查询语句 15 */ 16 public function __construct($config) { 17 $this->host=$config['host']; 18 $this->User=$config['user']; 19 $this->Pwd=$config['pwd']; 20 $this->Dbname=$config['dbname']; 21 $this->my_sql=$config['sql'];
//这个也是摘过来的的(if) 22 //if ( is_null(self::$_dbh) ) { 23 $this->link= $this->_connect(); 24 // } 25 $this->result= $this->Query($this->my_sql); 26 27 } 28 29 //成员方法 是用来执行sql语句的方法 30 /* 31 * 数据库查询函数 32 * $sql string 是你的查询语句 33 */ 34 public function Query($sql) 35 //两个参数:sql语句,判断返回1查询或是增删改的返回 36 { 37 $db = $this->connect(); 38 $r = $db->query($sql); 39 if (isset($r)) { 40 return $r->fetch_all();//查询语句,返回数组.执行sql的返回方式是all,也可以换成row 41 } else { 42 return "数据库查询失败!"; 43 } 44 45 46 } 47 /* 48 * 数据库连接函数 49 */ 50 public function connect(){ 51 $Link= mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname);
//$this->dbh= 52 return $Link; 53 } 54 55 } 56 //$sql='select * from zixun;'; 57 //$config=include './BBB.php'; 58 // $shujuku=new db($config); 59 60 61 // include './login.html'; 62 //var_dump($shujuku->result); 63 64 ?>
我的login.html代码展示:
<!-- 模板文件,利用HTML代码展示数据 --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>比赛列表</title> </head> <body> <table> <tr> <th>ZX_id</th><th>ZX_name</th><th>ZX_fenlei</th><th>ZX_zuozhe</th><th>更新时间</th><th>浏览次数</th><th>发布状态</th> </tr> <?php foreach($shujuku->result as $row) : ?> <tr> <td><?php echo $row[0];?></td> <td><?php echo $row[1];?></td> <td><?php echo $row[2];?></td> <td><?php echo $row[3];?></td> <td><?php echo $row[4];?></td> <td><?php echo $row[5];?></td> <td><?php echo $row[6];?></td> </tr> <?php endForeach;?> </table> </body> </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,'草稿');