1. PDO方式连接数据库类
<?php

/**
* @author 黄功延
* createTime 2018/5/28 0028 09:44
*/
class Db {
//私有化数据库连接数据,可以通过getInstance传入自己的连接数据来进行指定数据库连接(写框架的基础)
private $config = [
'dbname' => 'shunk',
'username' => 'root',
'password' => 'root'
]; private static $instance =null;
private $conn = null; //私有化构造方法,防止外部实例化这个类,即防止 new Db()
private function __construct() {
$this->connect();
}
//私有化克隆方法,防止外部克隆复制这个类
private function __clone() {
// TODO: Implement __clone() method.
}
//保证继承单一性,外部只能通过这个静态方法访问这个类
public static function getInstance(){ if(!(self::$instance instanceof self)){
self::$instance = new self();
}
return self::$instance;
} //PDO方式连接数据库的方法
private function connect(){
try{
$dns = 'mysql:dbname='.$this->config['dbname'].';host=localhost;port=3306;charset=utf8';
$this->conn = new PDO($dns,$this->config['username'],$this->config['password']);
$this->conn->query('SET NAMES utf8');
}catch (PDOException $e){
die('数据库连接失败'.$e->getMessage());
}
}
//查询一条记录
public function fetch($sql){ return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC);
}
//查询多条记录
public function fetchAll($sql){ return $this->conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
//增删改方法,删改成功返回受影响的行数。插入成功返回插入的id值
public function exec($sql){
$num = $this->conn->exec($sql);
if($num){
if('0' != $this->conn->lastInsertId()){
return $this->conn->lastInsertId();
}
return $num;
}else{
$error = $this->conn->errInfo();
return '操作失败'.$error[0].':'.$error[1].','.$error[2];
} }
}

   2.缓存PHP文件

<?php
/**
* @author 黄功延
* createTime 2018/5/28 0028 09:06
*/ $fileName = '../runtime/fileCache.php';
$time = 15;
//判断缓存文件是否存在和缓存文件是否过期
if(file_exists($fileName) && (filemtime($fileName)+$time) >= time()){
include ('../runtime/fileCache.php');
}else{
ob_start(); //开启内存缓存
include ('../Db.php'); $db = Db::getInstance();
$sql = 'select * from sk_url';
$info = $db->fetchAll($sql); include 'index1.php';
$str = ob_get_contents(); //得到缓存区内容
file_put_contents($fileName,$str); //写入缓存 ob_flush(); //关闭内存缓存
}

  3.html文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body> <table width="600" border="1" style="width: 700px; margin: 0 auto;">
<thead>
<tr>
<th>id</th>
<th>url</th>
<th>short</th>
<th>status</th>
<th>create_time</th>
</tr>
</thead>
<tbody>
<?php foreach ($info as $v){;?>
<tr>
<td><?php echo $v['id'];?></td>
<td><?php echo $v['url'];?></td>
<td><?php echo $v['short'];?></td>
<td><?php echo $v['status'];?></td>
<td><?php echo $v['create_time'];?></td>
</tr>
<?php };?>
</tbody> </table>
</body>
</html>

到此文件缓存的简单例子就完成了

最新文章

  1. [工具.tfs]可视化的TFS命令工具——Team Foundation Sidekicks
  2. JSP 原理
  3. 昨天的这个先补上--这个是关于 JQ 的移动 和 渐变特效的点击事件
  4. Boot loader: Grub进阶[转]
  5. Eclipse安装配置以及java项目和类的创建
  6. Digest [information value filtering]
  7. 转 如何使用velocity模板引擎开发网站
  8. POJ 2632 Crashing Robots(较为繁琐的模拟)
  9. 百度地图 api 功能封装类 (ZMap.js) 本地搜索,范围查找实例
  10. 处理Block中的self问题(Capturing &#39;self&#39; strongly in this block is likely to lead to a retain cycle)
  11. json数据渲染表单元素出现的问题
  12. Linux shell 脚本(二)
  13. LVM备份(3)- pg_dumpall
  14. 使用element-ui制作三级级联城市选择器
  15. linux查看网络信息命令
  16. 004 作业二(单击弹跳li节点的每个文本节点的值;点击每个 li 节点, 若 li 节点的文本值没有 ^^ 开头, 加上,有,则去除)
  17. #HTTP协议学习# (八)状态码详解
  18. Geometry
  19. Unity3D 中判断点与多边形的关系
  20. java-request与response编码问题

热门文章

  1. java byte/short/char补充(了解)
  2. 10.方法重写Override
  3. 清晰架构(Clean Architecture)的Go微服务: 事物管理
  4. 异数OS TCP协议栈测试(一)--数据传输篇
  5. 异步查询转同步加redis业务实现的BUG分享
  6. jvm内存模型、常见参数及调优
  7. HTTP负责均衡模块(HTTP Upstream)
  8. freemark 基本使用
  9. Vue使用better-scroll左右菜单联动
  10. 实现一个简易的RPC