1)小案例步骤一

首先:  

    MysqlDB类:

          作用:

              里面存的是mysql的操作函数,都是封装好的。

          疑问点:

              需要辨析的两个变量,就是$link是一个选好了数据库的MysqlDB对象,那个$recoure是一个只是连接了数据库的对象

    Model类:

          作用:

              其实也是操作数据库的,只不过它是基础的功能,就是取得MysqlDB类的实例化对象_dao    ,并且在实例化Model时就产生了_dao对象,因为在_construct

          疑问点:

              所有的model类都是继承这个Model类,所以都可以使用_dao类, $this->_dao->MysqlDB里面的方法

                                                                               比如:$this->_dao->getAll($config);

    zixun.controller类:

          作用:

              就是调控zixun.model.class.php文件和对应的HTML文件。

          注意点:

              在类的结束后,要将这个类实例化,才会得到你要的结果。

首先是文件关系:

                                 

    

 框架关系展示:

          

代码展示:

(1)首先是zixun.controller.class.php

 1 <?php
 2 //header('Content-type:text/html;charset=utf8');
 3     /**
 4      * Created by PhpStorm.
 5      * User: Interact
 6      * Date: 2017/8/19
 7      * Time: 18:37
 8      */
 9     class zixun{
10     public static function  show(){
11         require 'zixun.model.class.php';
12         $zixunModel=new zixunModel();
13         $records=$zixunModel->getall();
14 //       var_dump($records);
15         require 'html/show.html';
16         
17     }
18     }
19 zixun::show();

 (2)zixun.model.class.php代码展示:

 1 <?php
 2     /**
 3      * Created by PhpStorm.
 4      * User: Interact
 5      * Date: 2017/8/19
 6      * Time: 18:37
 7      */
 8     //首先是编写一个控制器操作类
 9     /**
10      * @return array
11      */
12     require 'Model.class.php';
13     class zixunModel extends Model{
14         /**
15          * @return mixed
16          */
17         public function  getall(){
18             $sql='select * from zixun';
19          return  $this->_dao->getAll($sql);
20         }
21     }
22     

(3)html代码展示:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="utf-8"><!-- 编码格式是 utf-8 -->
 5     <meta http-equiv="X-UA-Compatible" content="IE=edge"><!-- 使用最新式 ie 内核渲染国内某些 所谓的 双核浏览器 或者是 直接 使用webkit去渲染-->
 6     <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
 7     <!-- 视口属性没有设置 禁用 用户缩放, 如果有需求可以添加-->
 8     <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
 9     <title>bootstrap的模板页</title>
10     <!-- Bootstrap -->
11 
12 </head>
13 <body>
14 <table>
15     <tr>
16        <th>ID&nbsp&nbsp&nbsp</th>
17         <th>名字&nbsp&nbsp&nbsp</th>
18         <th>分类&nbsp&nbsp&nbsp</th>
19         <th>作者&nbsp&nbsp&nbsp</th>
20         <th>更新时间&nbsp&nbsp</th>
21         <th>浏览次数&nbsp&nbsp</th>
22         <th>发布状态&nbsp&nbsp</th>
23     </tr>
24     <?php foreach($records as $row) : ?>
25     <tr>
26         <th><?php echo $row['ZX_id']; ?></th>
27         <th><?php echo $row['ZX_name']; ?></th>
28         <th><?php echo $row['ZX_fenlei']; ?></th>
29         <th><?php echo $row['ZX_zuozhe']; ?></th>
30         <th><?php echo $row['gengxin_time']; ?></th>
31         <th><?php echo $row['liulan_cishu']; ?></th>
32         <th><?php echo $row['fabu_zhuangtai']; ?></th>
33     </tr>
34     <?php endforeach ?>
35 </table>
36 </body>
37 </html>

(4)Model代码展示:

 1 <?php
 2     /**
 3      * Created by PhpStorm.
 4      * User: Interact
 5      * Date: 2017/8/19
 6      * Time: 19:30
 7      */
 8     /*
 9      * 基础模型类
10      */
11 class Model{
12     protected $_dao;//就是数据库连接对象,可以在子类中用。
13     protected  function  _initDAO(){
14         //初始化MySQLDB
15         $config=array('host' => '127.0.0.1',    'port' => '3306', 'username'=>'root', 'password' => 'root', 'charset'=>'utf8', 'dbname'=>'thkphp5');
16         require_once 'MysqlDB.class.php';
17         $this->_dao = MysqlDB::getInstance($config);//$dao , Database Access Object 数据库操作对象(dao层)
18     }
19     /**
20      * 构造方法
21      * 为啥将上面那个方法在__construct()函数里,就是为了叫这个方法自动调用,到时候,我们得数据库连接对象就已经有了。
22      */
23     public function __construct() {
24         // 初始化DAO
25         $this->_initDAO();
26     }
27 }

(5)MysqlDB代码展示:

  1 <?php
  2     /**
  3      * Created by PhpStorm.
  4      * User: Interact
  5      * Date: 2017/8/19
  6      * Time: 19:32
  7      */
  8 class MysqlDB{
  9     public  $host;
 10     public $port;
 11     public  $username;
 12     public $passsword;
 13     public $charset;
 14     public $dbname;
 15     //数据库连接对象
 16     private static $link;//防止未接破坏这个连接对象,这个link就是MysqlDB 对象
 17     private $resourc;
 18     /*
 19      * @param $config,你的配置数组
 20      * @return 获取数据库连接对象$link,同时作为返回值
 21      */
 22     public static function getInstance($config){
 23         if(!isset(self::$link)){
 24             self::$link = new self($config);
 25             //或者是  self::$link=$this->__construct($config);
 26         }
 27         return self::$link;
 28     }
 29     //构造函数,禁止new,这样可以用工厂函数来创造类
 30     private  function  __construct($config) {
 31         $this->host=isset($config['host'])?$config['host']:'localhost';
 32         $this->port = isset($config['port']) ? $config['port'] : '3306';
 33         $this->username = isset($config['username']) ? $config['username'] : 'root';
 34         $this->password = isset($config['password']) ? $config['password'] : '';
 35         $this->charset = isset($config['charset']) ? $config['charset'] : 'utf8';
 36         $this->dbname = isset($config['dbname']) ? $config['dbname'] : '';
 37         //连接数据库
 38         $this->connect();
 39         //设定连接编码
 40         //$this->setCharset($this->charset);//这个执行不了,可能新的php有了更改
 41         //选定数据库
 42         $this->selectDb($this->dbname);
 43     }
 44     //禁止克隆
 45     private function __clone(){}
 46     public function connect(){
 47         $this->resourc = mysqli_connect("$this->host", "$this->username","$this->password") or die("连接数据库失败!");
 48     }
 49     
 50     public function selectDb($dbname){
 51         mysqli_select_db($this->resourc,$dbname);
 52     }
 53     /**
 54      * 功能:执行最基本(任何)sql语句
 55      * 返回:如果失败直接结束,如果成功,返回执行结果
 56      */
 57     public function query($sql){
 58         if(!$result = mysqli_query($this->resourc,$sql))
 59         {
 60             echo ("<br />执行失败。");
 61             echo "<br />失败的sql语句为:" . $sql;
 62             echo "<br />出错信息为:" . mysqli_error($this->resourc);
 63             echo "<br />错误代号为:" . mysqli_errno($this->resourc);
 64             die();
 65         }
 66         return $result;
 67     }
 68     /**
 69      * 功能:执行select语句,返回2维数组
 70      * 参数:$sql 字符串类型 select语句
 71      */
 72     public function getAll($sql){
 73         $result = $this->query($sql);
 74         $arr = array();    //空数组
 75         while( $rec = mysqli_fetch_assoc( $result )){
 76             $arr[] = $rec;//这样就形成二维数组
 77         }
 78         return $arr;
 79     }
 80     //返回一行数据(作为一维数组)
 81     public function getRow($sql){
 82         $result = $this->query($sql);
 83         //$rec = array();
 84         if( $rec2 = mysqli_fetch_assoc( $result )){//返回下标为字段名的数组
 85             //如果fetch出来有数据(也就是取得了一行数据),结果自然是数组
 86             return $rec2;
 87         }
 88         return false;
 89     }
 90     //返回一个数据(select语句的第一行第一列)
 91     //比如常见的:select count(*) as c from XXX where ...
 92     public function getOne($sql){
 93         $result = $this->query($sql);
 94         $rec = mysqli_fetch_row($result);//返回下标为数字的数组,且下标一定是0,1,2, 3.....
 95         //如果没有数据,返回false
 96         if($result === false){
 97             return false;
 98         }
 99         return $rec[0];    //该数组的第一项。
100         
101     }
102 }

然后:结果展示: 

          

原文地址:https://www.cnblogs.com/xiaoyoucai/p/7397858.html