我的php文件和html文件的位置关系:
然后我的主php文件是b.php,我的那个配置文件是BBB.php,我的html文件是login.html
然后我的b.php代码展示:
<?php
class db
{
public $host ;//= "localhost";//定义默认连接方式
public $User;//= "root";//定义默认用户名
public $Pwd;//= "root";//定义默认的密码
public $Dbname ;//= "thkphp5";//定义默认的数据库名
public $my_sql;
public $link;
public $result;
/*
* 构造函数
* 主机名,使用者,使用者密码,数据库的名字,查询语句
*/
public function __construct($config) {
$this->host=$config['host'];
$this->User=$config['user'];
$this->Pwd=$config['pwd'];
$this->Dbname=$config['dbname'];
$this->my_sql=$config['sql'];
$this->link= $this->connect();
$this->result= $this->Query($this->my_sql);
} //成员方法 是用来执行sql语句的方法
/*
* 数据库查询函数
* $sql string 是你的查询语句
*/
public function Query($sql)
//两个参数:sql语句,判断返回1查询或是增删改的返回
{
$db = $this->connect();
$r = $db->query($sql);
if (isset($r)) {
return $r->fetch_all();//查询语句,返回数组.执行sql的返回方式是all,也可以换成row
} else {
return "数据库查询失败!";
} }
/*
* 数据库连接函数
*/
public function connect(){
$Link= mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname);
return $Link;
} }
$sql='select * from zixun;';
$config=include './BBB.php';
$shujuku=new db($config); include './login.html';
//var_dump($shujuku->result); ?>
我的BBB.php文件展示:
<?php
return $config=array(
'host'=>"localhost",
'user'=>"root",
'pwd'=>"root",
'dbname'=>"thkphp5",
'sql'=>$sql);
我的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[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
</tr>
<?php endForeach;?>
</table>
</body>
</html>
我的结果展示:
数据库代码展示:
CREATE DATABASE `thkphp5` ;
use thkphp5 ;
create table zixun(
ZX_id int not null auto_increment primary key comment '咨询ID号',
ZX_name VARCHAR() NOT NULL COMMENT '咨询标题',
ZX_fenlei varchar() not null comment '资讯分类',
ZX_zuozhe varchar() not null comment '资讯作者',
gengxin_time DATETIME NOT NULL DEFAULT '2016-01-01 01:01:01' COMMENT '更新时间',
liulan_cishu int NOT NULL COMMENT '浏览次数',
fabu_zhuangtai VARCHAR() NOT NULL COMMENT '发布状态'
)engine=MyISAM charset=utf8;
INSERT into zixun(ZX_id, ZX_name, ZX_fenlei, ZX_zuozhe, gengxin_time, liulan_cishu, fabu_zhuangtai) values(, 'PHP', '理论', '王超', '2017-08-07 11:58:01', , '草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'C语言','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'JAVA语言','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'Mysql语言','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'html','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'spring','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'scence','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'computer','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'math','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'english','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'word','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'jsp','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'CSS','理论','王超','2017-08-07 11:58:01',,'草稿');