1. 程式人生 > >1)小案例步驟一

1)小案例步驟一

其他 com 到你 ons inter show urn ec2 database

首先:

    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 }

然後:結果展示:

          技術分享

1)小案例步驟一