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

搜索
博主信息
博文 32
粉絲 0
評(píng)論 0
訪問(wèn)量 27813
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
08-01作業(yè):模仿課堂案例, 寫(xiě)一個(gè)鏈?zhǔn)降臄?shù)據(jù)庫(kù)查詢過(guò)程
Yx的博客
原創(chuàng)
1102人瀏覽過(guò)

.1..鏈?zhǔn)降臄?shù)據(jù)庫(kù)查詢實(shí)例:

因?yàn)橹皠?chuàng)建了PHP.sql,所以我們只要在數(shù)據(jù)庫(kù)中加入一張表即可,這里我加入表名 stud

02.png

下面是代碼部分:

鏈接Query.php代碼:

<?php

//這里我給空間命名為_(kāi)0804
namespace _0804;

class Query
{
    // 連接對(duì)象,給一個(gè)空值null
    public $pdo = null;

    // 數(shù)據(jù)表table,但是在編輯器里好像會(huì)報(bào)錯(cuò)說(shuō)數(shù)據(jù)表里沒(méi)有這個(gè)名稱
    public $table;

    public $field = '*';

    public $where;

    public $limit;

    // 構(gòu)造方法, 連接數(shù)據(jù)庫(kù)
    public function __construct($pdo)
    {
        $this->pdo = $pdo;
    }

    // 設(shè)置表名
    public function table($tableName)
    {
        $this->table = $tableName;
        //返回一個(gè)當(dāng)前類(lèi)實(shí)例
        return $this;
    }

    public function field($fields = '*')
    {
        $this->field = empty($fields) ? '*' : $fields;
        return $this;
    }

    public function where($where = '')
    {
        $this->where = empty($where) ? $where : ' WHERE ' . $where;
        return $this;
    }

    public function limit($limit)
    {
        $this->limit = empty($limit) ? $limit : ' LIMIT ' . $limit;
        return $this;
    }


    // SQL語(yǔ)句拼接
    public function select()
    {
        // 拼裝
        $sql = 'SELECT '
            . $this->field  // 字段
            . ' FROM '
            . $this->table    // 數(shù)據(jù)表
            . $this->where  //條件
            . $this->limit;

        // 預(yù)處理查詢
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();
//        die($stmt->debugDumpParams());
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }


}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例

<?php

namespace __0804;

require 'Query.php'; //引入Query.php

use _0804\Query;

class Db
{
    // 數(shù)據(jù)庫(kù)的連接對(duì)象
    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ù)庫(kù)
        self::connection();

        // 實(shí)例 化一個(gè)查詢類(lèi)Query.php
        $query = new Query(self::$pdo);

        // 執(zhí)行查詢類(lèi)中的方法
        return call_user_func_array([$query, $name], $arguments);
    }


}

//$staffs = Db::table('staff')->select();
$studs = Db::table('stud')
    ->field('stud_id, name,age,mobile')
    ->where('stud_id > 0')  //展示大于0的ID
    ->limit(5)
    ->select();

foreach ($studs as $stud) {
    print_r($stud); echo '<br>';
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


2.執(zhí)行后效果:

01.png



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

老師批語(yǔ):其實(shí)這個(gè)案例已經(jīng)涉及到一些設(shè)計(jì)模式的知識(shí)了, 后面的課程, 我們會(huì)介紹一些經(jīng)典的設(shè)計(jì)模式思想
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

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

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