还有一个加了单例模式的,在第52个。
首先是我的文件关系:
我的主php文件是index.php,我的配置文件php是BBB.php 我的数据库操作文件是 b.php 我的html文件是login.html
我的index.php代码展示:
<?php
$sql='select * from zixun;';
$config=include './BBB.php';
include './b.php'; $shujuku=new db($config);
include './login.html';
我的BBB.php代码展示:
<?php
return $config=array(
'host'=>"localhost",
'user'=>"root",
'pwd'=>"root",
'dbname'=>"thkphp5",
'sql'=>$sql);
我的b.php代码展示:
<?php
class db
{
public $host ;//= "localhost";//定义默认连接方式
public $User;//= "root";//定义默认用户名
public $Pwd;//= "root";//定义默认的密码
public $Dbname ;//= "thkphp5";//定义默认的数据库名
public $my_sql;
public $link;
public $result;
// protected static $_dbh = null; //静态属性,所有数据库实例共用,避免重复连接数据库,这个是学来的,看的别人的代码,觉得不错,摘过来的
/*
* 构造函数
* 主机名,使用者,使用者密码,数据库的名字,查询语句
*/
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'];
//这个也是摘过来的的(if)
//if ( is_null(self::$_dbh) ) {
$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);
//$this->dbh=
return $Link;
} }
//$sql='select * from zixun;';
//$config=include './BBB.php';
// $shujuku=new db($config); // include './login.html';
//var_dump($shujuku->result); ?>
我的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[];?></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',,'草稿');