view文件下的index.php <?php /** * 入口文件:前端控制器 */ //加载所有用到的类 require 'model/Db.php'; require 'model/Model.php'; require 'model/StudentModel.php'; //当前URL中有没有controller,无则默认Student $controller = isset($_GET['c'])?$_GET['c']:'Student'; //给控制器加后缀 $controller .= 'Controller'; //加载控制器 require 'controller/'.$controller.'.php'; //获取一下操作 $action = isset($_GET['a'])?$_GET['a']:'getAll'; $stu = new $controller(); $stu->$action(); controller文件下的StudentController.php <?php class StudentController { //获取所有数据 public function getAll() { //实例化模型,获取数据 $stu = new StudentModel(); $data = $stu->all(); print_r($data); } //获取单条数据 public function getInfo($id=1) { $id = isset($_GET['id']) ? $_GET['id'] : $id; //实例化模型,获取数据 $stu = new StudentModel(); $data = $stu->get($id); // print_r($data); } } model下面的db.php 封装数据库 <?php /** * 封装数据库操作 */ class Db { //配置参数 private $dbConfig = [ 'dbType'=>'mysql',//数据库类型 'host'=>'localhost',//服务器名称 'charset'=>'utf-8',//字符编码 'dbname'=>'demo',//数据库 'userName'=>'root',//用户名 'password'=>'root',//密码 ]; //新增主键ID public $insertIDd = null; //受影响的记录数 public $num = 0; //单例模式,当前类实例 private static $instance = null; //数据库连接对象 private $pdo; //构造方法 private function __construct($params=[]) { //使用用户自定义的参数来更新默认的参数 $this->dbConfig = array_merge($this->dbConfig,$params); //连接数据库 $this->connect(); } private function __clone() { // TODO: Implement __clone() method. } //获取当前类的唯一实例 public static function getInstance($params=[]) { if (!self::$instance instanceof self) { self::$instance = new self($params); } return self::$instance; } //连接数据库 private function connect() { try{ //配置DSN $dsn = $this->dbConfig['dbType'] .':host='.$this->dbConfig['host'] .';dbname='.$this->dbConfig['dbname'] .';charset='.$this->dbConfig['charset']; $userName = $this->dbConfig['userName']; $password = $this->dbConfig['password']; //创建PDO对象,连接数据库 $this->pdo = new PDO($dsn,$userName,$password); }catch (PDOException $e) { print $e->getMessage(); die(); } } //写操作:新增,更新,删除,exrc() public function exec($sql) { $num = $this->pdo->exec($sql); if ($num >0) { if (null !==$this->pdo->lastInsertId()) {//新增操作 $this->insertIDd = $this->pdo->lastInsertId(); } $this->num = $num;//更新,删除 }else{ print_r($this->pdo->errorInfo()); } } //获取单条记录 public function get($sql) { $pdoStmt = $this->pdo->query($sql); if (isset($pdoStmt) && $pdoStmt->rowCount()>0) { return $pdoStmt->fetch(PDO::FETCH_ASSOC); }else { print_r($this->pdo->errInfo()); } } //获取所有记录 public function all($sql) { $pdoStmt = $this->pdo->query($sql); if (isset($pdoStmt) && $pdoStmt->rowCount()==1) { return $pdoStmt->fetchAll(PDO::FETCH_ASSOC); }else { print_r($this->pdo->errInfo()); } } } model文件夹下面的model.php <?php /** * 公共模型类 */ class Model { protected $db = null;//数据库连接对象 public $data = null;//当前用户请求的数据 public function __construct() { $this -> init();//初始化就是连接数据库 } private function init() { $dbConfig = [ 'userName'=>'root', 'password'=>'root', 'dbname'=>'demo' ]; //用自定义的连接对象 $this->db = Db::getInstance($dbConfig); } //获取所有数据 public function all() { $sql = "SELECT id,name FROM demo"; return $this->data = $this->db->all($sql); } //获取单条数据 public function get($id=1) { $sql = "SELECT id,name FROM demo WHERE id={$id}"; return $this->data = $this->db->get($sql); } } model下面的test_db.php测试增删改查 <?php //测试db类增删改查 include 'db.php'; $db = Db::getInstance(); echo 'pre'; //获取id=5的记录 $sql = 'SELECT * FROM demo WHERE id=5'; print_r($db->get($sql)); //获取多条记录 $sql = 'SELECT * FROM demo'; print_r($db->all($sql)); //新增操作 $sql = "INSERT demo SET name='peter',email='aa@qq.com',"; $db->exec($sql); echo '新增主键'.$db->insertIDd,',插入了'.$db->num.'条'; //更新操作 $sql = "UPDATE demo SET name='peterZHU' id=8"; $db->exec($sql); echo '更新了'.$db->num.'条'; //删除操作 $sql = 'DELETE FROM demo WHERE id=8'; $db->exec($sql); echo '删除了'.$db->num.'条';