亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 36
粉絲 4
評論 3
訪問量 38366
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
8.1 php鏈式數(shù)據(jù)庫查詢過程
大灰狼的博客
原創(chuàng)
1049人瀏覽過

php鏈式數(shù)據(jù)庫查詢過程

使用了魔術變量 __callStatic特性 當對找不到對應方法時便會執(zhí)行方法,在__callStatic()方法內(nèi)在使用call_user_func_array()回調(diào) 調(diào)用找對應的方法來執(zhí)行。

小案例插曲 :平時寫PDO后會使用var_dump()快速測試是否鏈接成功,若返回 object(PDO)#1 (0) 會認為已經(jīng)鏈接成功!但并非如此 就算返回這個也不代表成功。。。當時就是默寫少了一個關鍵詞 但測試鏈接成功 所以分段 分塊調(diào)試很重要得冷靜思考為啥查詢語句正確不返回數(shù)據(jù)應該就是 鏈接問題。

self::$pdo=new \PDO('mysql:127.0.0.1;dbname=php','root','root'); //錯誤語句 少關鍵詞


下來看下數(shù)據(jù)庫鏈式查詢小案例圖
1.jpg

Query.php

實例

實例

<?php


namespace _chaxun;


class Query2
{
    //鏈接對象
    public $pdo=null;
    //數(shù)據(jù)表
    public $table;
    //返回字段
    public $field='*';
    //查詢條件
    public $where;
    //數(shù)據(jù)排序
    public $order;
    //顯示數(shù)量
    public $limit;

    //構造方法,鏈接數(shù)據(jù)庫
    public function __construct($pdo)
    {
        $this->pdo=$pdo;
    }
    //設置表名
    public function table($tableName)
    {
        $this->table=$tableName;
        return $this;//關鍵性動作 返回一個當前類的實例(返回自己)
    }
    //返回字段
    public function field($fields='*')
    {
        $this->field=empty($fields) ? '*':$fields;
        return $this;
    }
    //數(shù)據(jù)排序
    public function order($order='')
    {
        $this->order=empty($prder) ? ' ORDER BY '.$order:'';
        return $this;
    }
    //查詢條件
    public function where($where='')
    {
        $this->where=empty($where) ? $where : ' WHERE '.$where;
        return $this;
    }
    //顯示數(shù)量
    public function limit($limit)
    {
        //支持偏移量與查詢條數(shù)[5,6] 也可以單獨限制條數(shù)
        is_array($limit)? $limit="{$limit[0]},{$limit[1]}" :$limit;
        $this->limit = empty($limit) ? $limit : ' LIMIT ' . $limit;
        return $this;
    }

    //拼裝SQL語句
    public function select()
    {
        //拼接
        $sql='SELECT '
          .$this->field //返回字段
          .' FROM '
          .$this->table //數(shù)據(jù)表
          .$this->where //查詢條件
          .$this->order //排序
          .$this->limit; //查詢字段
        //預處理查詢
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();
//        die($stmt->debugDumpParams()); //錯誤調(diào)試 查看sql語句
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);

    }

}

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例


index.php

實例

<?php
namespace _0801;

require 'Query.php';
use _chaxun\Query2;

class Db
{
    //數(shù)據(jù)庫的鏈接對象
    protected static $pdo=null;
    //鏈接方法
    public static function connection()
    {
        self::$pdo=new \PDO('mysql:host=127.0.0.1;dbname=php','root','root');
    }
    public static function __callStatic($name,$arguments)
    {
        //鏈接數(shù)據(jù)庫
        self::connection();
        //實例化一個查詢類,
        $query=new Query2(self::$pdo);
        //執(zhí)行查詢類中的方法
        return call_user_func_array([$query,$name],$arguments);

    }

}

$datas=Db::table('staff')//查詢staff表
    ->field('age,name,mobile')//返回 age,name ,mobile字段的數(shù)據(jù)
    ->order('age desc')//以年齡降序排序
    ->limit([3,50])//查詢50條數(shù)據(jù) 從第4條開始
    ->where('age>35 AND mobile LIKE "%13%"')//年齡大于35歲 并且手機號包含13數(shù)字
    ->select();

//var_dump($datas);

foreach ($datas as $data){
    print_r($data);
    echo '<br>';
}

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例




批改狀態(tài):合格

老師批語:limit 后面最多二個值, 沒必要用數(shù)組
本博文版權歸博主所有,轉(zhuǎn)載請注明地址!如有侵權、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務協(xié)議
0條評論
作者最新博文
關于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓,幫助PHP學習者快速成長!
關注服務號 技術交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學習
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學習!
    全站2000+教程免費學