成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

靜態(tài)PDO基礎(chǔ)操作封裝

Mr_zhang / 3461人閱讀

摘要:環(huán)境要求先貼代碼空城空城基本使用兼容兩種寫法自動(dòng)綁定與參數(shù)形式參數(shù)形式寫法優(yōu)先于自動(dòng)綁定自動(dòng)綁定寫法初始化引入文件取別名可省略連接信息可省略可在文件內(nèi)寫好配置綁定操作信息開(kāi)始使用該操作最終執(zhí)

環(huán)境要求:PHP7.1 PDO Extension
先貼代碼

 * Copyright 2017 空城
 * CreateDate 2017-12-14
 */

class mysql_pdo
{
    // Host address
    public static $host     = "127.0.0.1";
    // Host port
    public static $port     = 3306;
    // Username
    public static $user     = "root";
    // Password
    public static $pwd      = "root";
    // Database
    public static $db       = "";
    // Character set
    public static $charset  = "utf8mb4";
    // Persistent connection
    public static $pconnect = true;
    // Connection object
    public static $conn     = null;
    // Table name
    public static $table    = "";

    // Core container
    public static $data     = "";
    public static $field    = "*";
    public static $where    = "";
    public static $order    = "";
    public static $group    = "";
    public static $limit    = "";
    public static $join     = "";
    public static $bind     = [];
    public static $sql      = "";

    // Initialization
    public static function init(array $conf = array(), bool $reconnect = false):void
    {
        class_exists("PDO") or exit("PDO: class not exists.");
        empty($conf["host"])  or self::$host  = $conf["host"];
        empty($conf["port"])  or self::$port  = $conf["port"];
        empty($conf["user"])  or self::$user  = $conf["user"];
        empty($conf["pwd"])   or self::$pwd   = $conf["pwd"];
        empty($conf["db"])    or self::$db    = $conf["db"];
        empty($conf["table"]) or self::$table = $conf["table"];
        if (is_null(self::$conn) || $reconnect) self::$conn = self::_connect();
    }

    // Query or Exec
    public static  function do(string $sql = "", bool $flag = false)
    {
        empty($sql) or self::$sql = $sql;
        $preg = "INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|SELECT .* INTO|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK"; 
        if (preg_match("/^s*"?(" . $preg . ")s+/i", self::$sql)) return self::exec("", $flag);
        else return self::query("", self::$sql);
    }

    // Query
    public static function query(string $sql = "", bool $flag = false):array
    {
        $statm = self::_start($sql);
        $result = $statm->fetchAll(PDO::FETCH_ASSOC);
        return $flag ? $result[0] : $result;
    }

    // Exec
    public static function exec(string $sql = "", bool $flag = false):int
    {
        $statm = self::_start($sql);
        $row = $statm->rowCount();
        return $flag ? self::$conn->lastInsertId() : $row;
    }

    // Insert 
    public static function insert(string $table = "", array $data = [], bool $flag = false):int
    {

        $table = !empty($data) ? $table : self::$table;
        $data = !empty($data) ? $data : self::$data;
        $insertData = [];
        if ( count($data) == count($data, 1) ) $insertData[0] = $data;
        else $insertData = $data;
        $lastId = 0;
        $row = 0;
        foreach ($insertData as $key => $data) {
            $data = self::_format($table, $data);
            $vals = [];
            foreach ($data as $key => $value) {
                $vals[] = self::_setBind(str_replace("`", "", $key), $value);
            }
            $keys = array_keys($data);
            
            self::$sql = "INSERT INTO `" . trim($table) . "` (" . implode(",", $keys) . ") VALUES(" . implode(",", $vals) . ")";
            self::exec() && $flag && $row += 1;
        }
        
        $lastId = self::$conn->lastInsertId();
        unset($insertData,$data);
        return $flag ? $row : $lastId;
    }

    // Delete
    public static function del(string $table = "", array $where = []):int
    {
        $table = !empty($data) ? $table : self::$table;
        $where = !empty($where) ? self::_where($where) : self::_where(self::$where);
        if ("" === $where) return 0;
        self::$sql = "DELETE FROM `".trim($table)."` ".$where;
        unset($table, $where);
        return self::exec();
    }

    // Update
    public static function save(string $table = "", array $data = [], $where = []):int
    {
        $table = !empty($table) ? $table : self::$table;
        $data = !empty($data) ? $data : self::$data;
        $where = !empty($where) ? $where : self::$where;
        if (false == $where) {
            $key = self::_tbKey($table);
            $where = [];
            foreach ($key as $k => $v) {
                empty($data[$k]) or $where[$k] = $data[$k];
            }
            $where = self::_where($where);
        } else $where = self::_where($where);
        $data = self::_format($table, $data);
        $kv = [];
        foreach ($data as $key => $value) {
            $k = str_replace("`", "", $key);
            $k = self::_setBind($k, $value);
            $kv[] = $key."=".$k;
        }
        $kv_str = implode(",", $kv);
        self::$sql = "UPDATE `".trim($table)."` SET ".trim($kv_str)." ".trim($where);
        unset($kv_str, $data, $kv, $table);
        if ("" === $where) return 0;
        return self::exec();
    }

    // Select
    public static function select(string $table = "", array $opt = []):array
    {
        $opt = self::_condition($table, $opt);
        $field = $opt["field"] = !empty($opt["field"]) ? $opt["field"] : self::$field;
        if (is_array($field)) {
            foreach ($field as $key => $value) $field[$key] = self::_avoidKey($value);
            $field = implode(",", $field);
        }
        elseif(is_string($field) && $field != "");
        else $field = "*";
        self::$sql = "SELECT ".$field." FROM `".$opt["table"]."` ".$opt["join"].$opt["where"].$opt["group"].$opt["order"].$opt["limit"];
        unset($opt);
        return self::query();
    }

    // Get a line
    public static function first(string $table = "", array $opt = []):array
    {
        self::$limit = "1";
        $result = self::select($table, $opt);
        return $result[0];
    }

    // Count
    public static function count(string $table = "", array $opt = []):array
    {
        $option = self::_condition($table,$opt);
        return self::_common($option, "count");
    }

    // Avg
    public static function avg(string $table = "", array $opt = []):array
    {
        $option = self::_condition($table,$opt);
        return self::_common($option, "avg");
    }

    // Sum
    public static function sum(string $table = "", array $opt = []):array
    {
        $option = self::_condition($table,$opt);
        return self::_common($option, "sum");
    }

    // Min
    public static function min(string $table = "", array $opt = []):array
    {
        $option = self::_condition($table,$opt);
        return self::_common($option, "min");
    }

    // Max
    public static function max(string $table = "", array $opt = []):array
    {
        $option = self::_condition($table,$opt);
        return self::_common($option, "max");
    }

    // Dec
    public static function dec(string $table = "", $data = [], $where = []):int
    {
        return self::_setCol($table, $data, $where,"-");
    }

    // Inc
    public static function inc(string $table = "", $data = [], $where = []):int
    {
        return self::_setCol($table, $data, $where,"+");
    }

    // Clear
    public static function clear():void
    {
        self::$data  = "";
        self::$field = "*";
        self::$where = "";
        self::$order = "";
        self::$group = "";
        self::$limit = "";
        self::$bind  = [];
    }

    // SetAttribute
    public static function setAttr($key, $val):bool
    {
        !empty(self::$conn) or self::_connect();
        return self::$conn->setAttribute($key, $val);
    }

    // BeginTransaction
    public static function begin():bool
    {
        !empty(self::$conn) or self::_connect();
        return self::$conn->beginTransaction();
    }

    // Commit
    public static function commit():bool
    {
        !empty(self::$conn) or self::_connect();
        return self::$conn->commit();
    }

    // RollBack
    public static function rollBack():bool
    {
        !empty(self::$conn) or self::_connect();
        return self::$conn->rollBack();
    }

    // Connect
    protected static function _connect():void
    {
        $dsn = "mysql:host=".self::$host.";port=".self::$port.";dbname=".self::$db;
        $options = [
                PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES " . self::$charset,
                PDO::ATTR_PERSISTENT         => (bool)self::$pconnect
            ];
        try { 
            $dbh = new PDO($dsn, self::$user, self::$pwd, $options);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $dbh->exec("SET NAMES " . self::$charset);
        } catch (PDOException $e) { 
            exit("Connection failed: " . $e->getMessage());
        }
        self::$conn = $dbh;
        unset($dsn, $dbh, $options);
    }

    // Mosaic SQL
    protected static function _condition(string $table, array $opt):array
    {
        $option = [];
        $option["table"] = !empty($table) ? $table : self::$table;
        $option["field"] = !empty($opt["field"]) ? $opt["field"] : self::$field;
        $option["join"] = !empty($opt["join"]) ? self::_join($opt["join"]) :self::_join(self::$join);
        $option["where"] = !empty($opt["where"]) ? self::_where($opt["where"]) : self::_where(self::$where);
        $option["order"] = !empty($opt["order"]) ? self::_order($opt["order"]) : self::_order(self::$order);
        $option["group"] = !empty($opt["group"]) ? self::_group($opt["group"]) : self::_group(self::$group);
        $option["limit"] = !empty($opt["limit"]) ? self::_limit($opt["limit"]) : self::_limit(self::$limit);
        return $option;
    }

    // Exec SQL common function
    protected static function _start(string $sql = "")
    {
        empty($sql) or self::$sql = $sql;
        !empty(self::$conn) or self::_connect();
        $statm = self::$conn->prepare(self::$sql);
        $statm->execute(self::$bind);
        self::clear();
        return $statm;
    }

    // Common
    protected static function _common(array $opt, string $func):array
    {
        if (is_string($opt["field"]) && $opt["field"] != "") {
            $strField = $opt["field"];
            $fieldArr = explode(",", $strField);
            $strField = "_".implode("_,_", $fieldArr)."_";
        } elseif (is_array($opt["field"])) {
            $fieldArr = $opt["field"];
            $strField = "_".implode("_,_", $opt["field"])."_";
        } else return false;

        foreach ($fieldArr as $v) {
            $val = self::_avoidKey($v);
            $alias = str_replace(".", "_", $val);
            $alias = " AS ".(false === strpos($val, "*") ? $alias : "`". $alias ."`");
            $strField = str_replace("_".$v."_", $func . "(" . $val . ") ".$alias, $strField);
        }
        self::$sql = "SELECT ".$strField." FROM `".$opt["table"]."` ".$opt["join"].$opt["where"].$opt["group"].$opt["order"].$opt["limit"];
        unset($opt, $func, $fieldArr, $strField, $alias);
        $result = self::query();
        return count($result) == 1 && !empty($result[0]) ? $result[0] : $result;
    }

    // Set field
    protected static function _setCol(string $table = "", $data = "", $where = [], string $type):int
    {
        $table = !empty($table) ? $table : self::$table;
        $data  = !empty($data) ? $data : self::$data;
        $where = !empty($where) ? self::_where($where) : self::_where(self::$where);

        if (is_array($data)) {
            $new_data = [];
            foreach ($data as $key => $value) {
                if (is_string($key)) $new_data[$key] = $key.$type.abs($value);
                else $new_data[$value] = $value.$type."1";
            }
        } elseif (is_string($data)) $new_data[$data] = $data.$type."1";
        $kv = [];
        foreach ($new_data as $key => $value) {
            $kv[] = self::_avoidKey($key)."=".$value;
        }
        $kv_str = implode(",", $kv);
        self::$sql = "UPDATE `".trim($table)."` SET ".trim($kv_str)." ".trim($where);
        unset($data);
        if ("" === $where) return 0;
        return self::exec();
    }

    // Preprocessing
    protected static function _setBind(string $key, $value):string
    {
        if (empty(self::$bind[":".$key])) {
            $k = ":".$key;
            self::$bind[$k] = $value;
        } else {
            $k = ":".$key."_".mt_rand(1,9999);
            while (!empty(self::$bind[":".$k])) {
                $k = ":".$key."_".mt_rand(1,9999);
            }  
            self::$bind[$k] = $value;
        }
        unset($key, $value);
        return $k;
    }

    // Join
    protected static function _join($opt):string
    {
        $join = "";
        if (is_string($opt) && "" !== trim($opt)) return $opt;
        elseif (is_array($opt)) {
            foreach($opt as $key => $value) {
                $mode = "INNER";
                if (is_array($value)) {
                    if (!empty($value[2]) && 0 === strcasecmp($value[2], "LEFT")) $mode = "LEFT";
                    elseif (!empty($value[2]) && 0 === strcasecmp($value[2], "RIGHT")) $mode = "RIGHT";
                    $relative = !empty($value[3]) ? $value[3] : "=";
                    $condition = " ".$mode." JOIN ".$key." ON ".self::_avoidKey($value[0]).$relative.self::_avoidKey($value[1])." ";
                } else {
                    $condition = " ".$mode." JOIN ".$key." ON ".$value." ";
                }
                $join .= $condition;
            }
        }
        unset($opt);
        return $join;
    }

    // Where
    protected static function _where($opt):string
    {
        $where = "";
        if (is_string($opt) && "" !== trim($opt)) return " WHERE ".$opt;
        elseif (is_array($opt)) {
            foreach($opt as $key => $value) {
                $k = self::_avoidKey($key);
                if (is_array($value)) {
                    $key = self::_setBind($key,$value[0]);
                    $relative = !empty($value[1]) ? $value[1] : "=";
                    $link    = !empty($value[2]) ? $value[2] : "AND";
                    $condition = " (".$k." ".$relative." ".$key.") ";
                } else {
                    $key = self::_setBind($key,$value);
                    $link = "AND";
                    $condition = " (".$k."=".$key.") ";
                }
                $where .= $where !== "" ? $link.$condition : " WHERE ".$condition;
            }
        }
        unset($opt);
        return $where;
    }

    // Order
    protected static function _order($opt):string
    {
        $order = "";
        if (is_string($opt) && "" !== trim($opt)) return " ORDER BY "._avoidKey($opt);
        elseif (is_array($opt)) {
            foreach($opt as $key => $value) {
                $link = ",";
                if (is_string($key)) {
                    if (0 === strcasecmp($value, "DESC")) $condition = " ".self::_avoidKey($key)." DESC ";
                    else $condition = " ".self::_avoidKey($key)." ASC ";
                }  else $condition = " ".self::_avoidKey($value)." ASC ";
                $order .= $order !== "" ? $link.addslashes($condition) : " ORDER BY ".addslashes($condition);
            }
        }
        unset($opt);
        return $order;
    }

    // Limit
    protected static function _limit($opt):string
    {
        $limit = "";
        if (is_string($opt) && "" !== trim($opt)) return " LIMIT ".$opt;
        elseif (is_array($opt) && 2 == count($opt)) $limit = " LIMIT ".(int)$opt[0].",".(int)$opt[1];
        elseif (is_array($opt) && 1 == count($opt)) $limit = " LIMIT ".(int)$opt[0];
        unset($opt);
        return $limit;
    }

    // Group
    protected static function _group($opt):string
    {
        $group = "";
        if (is_string($opt) && "" !== trim($opt)) return " GROUP BY "._avoidKey($opt);
        elseif (is_array($opt)) {
            foreach($opt as $key => $value) {
                $link = ",";
                $condition = " ".self::_avoidKey($value)." ";
                $group .= $group !== "" ? $link.addslashes($condition) : " GROUP BY ".addslashes($condition);
            }
        }
        unset($opt);
        return $group;
    }

    // Format data
    protected static function _format(string $table, $data):array
    {
        if (!is_array($data)) return array();

        $tbColumn = self::_tbInfo($table);
        $res = [];
        foreach ($data as $key => $val) {
            if (!is_scalar($val)) continue;
            if (!empty($tbColumn[$key])) {
                $key = self::_avoidKey($key);
                if (is_int($val)) $val = intval($val);
                elseif (is_float($val)) $val = floatval($val); 
                elseif (preg_match("/^(w*(+|-|*|/)?w*)$/i", $val)) $val = $val;
                elseif (is_string($val)) $val = addslashes($val);
                $res[$key] = $val;
            }
        }
        unset($data);
        return $res;
    }

    // Table info
    protected static function _tbInfo(string $table = ""):array
    {
        $table = !empty($table) ? $table : self::$table;
        $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME="".$table."" AND TABLE_SCHEMA="".self::$db.""";
        !empty(self::$conn) or self::_connect();
        $statm = self::$conn->prepare($sql);
        $statm->execute();
        $result = $statm->fetchAll(PDO::FETCH_ASSOC);
        $res = [];
        foreach ($result as $key=>$value) {
            $res[$value["COLUMN_NAME"]] = 1;
        }
        // unset($result, $statm);
        return $res;
    }

    // Get primary key
    protected static function _tbKey(string $table):array
    {
        $sql = "SELECT k.column_name FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING (constraint_name,table_schema,table_name) WHERE t.constraint_type="PRIMARY KEY" AND t.table_schema="".self::$db."" AND t.table_name="".$table.""";
        !empty(self::$conn) or self::_connect();
        $statm = self::$conn->prepare($sql);
        $statm->execute();
        $result = $statm->fetchAll(PDO::FETCH_ASSOC);
        $res = [];
        foreach ($result as $key=>$value) {
            $res[$value["column_name"]] = 1;
        }
        unset($result, $statm);
        return $res;
    }

    // Avoid mistakes
    protected static function _avoidKey(string $value):string
    { 
        if ("*" == $value || false !== strpos($value,"(") || false !== strpos($value,"."));
        elseif (false === strpos($value,"`")) $value = "`".trim($value)."`";
        return $value; 
    }
}
基本使用

兼容兩種寫法,自動(dòng)綁定與參數(shù)形式,參數(shù)形式寫法優(yōu)先于自動(dòng)綁定

自動(dòng)綁定寫法:

Example-1:初始化

// step_1:引入文件
require_once "mysql.php";
// 取別名(可省略)
use mysql_pdo as db;
// 連接信息(可省略,可在文件內(nèi)寫好配置)
db::$host = "127.0.0.1";
db::$user = "root";
db::$pwd = "root";
db::$db = "test";
// step_2:綁定操作信息
db::$tbale = "tablename";
// step_3:開(kāi)始使用
db::select();  // 該操作最終執(zhí)行的語(yǔ)句是:SELECT * FROM `tablename`

Example-2:Insert操作

// 綁定操作表
db::$table = "tablename";
// 綁定數(shù)據(jù),如需插入多行數(shù)據(jù),構(gòu)建的數(shù)據(jù)為二維數(shù)組即可
db::$data = [
    "col_1" => "value_1",
    "col_2" => "value_2",
    "col_3" => "value_3",
    "col_4" => "value_4"
];
// 執(zhí)行
db::insert();  
// 返回值為最后插入的ID,如果要獲取插入的行數(shù)請(qǐng)使用傳參寫法 db::insert("tablename",$data,true) 參數(shù)形式無(wú)須綁定參數(shù)

Example-3:Delete操作

// 操作表,如果每次都對(duì)同一張表進(jìn)行操作,則不需要重新綁定
db::$table = "tablename";
// 綁定where條件,防止刪表操作,在沒(méi)有where條件下不會(huì)執(zhí)行delete操作
db::$where = ["id" => 1] // 數(shù)組形式一 最終解析:[AND] `id` = 1 默認(rèn)對(duì)比關(guān)系為 =,邏輯 AND
db::$where = ["id" => ["1",">","AND"]] // 數(shù)組形式二 最終解析:[AND] `id` > 1
db::$where = "id = 1"  //字符串形式 最終解析為: WHERE id = 1
// 執(zhí)行
db::del();

Example-4:Update操作

 // 在綁定表的基礎(chǔ)上綁定數(shù)據(jù)即可
db::$data = [
    "col_1" => "value_1",
    "col_2" => "update_2",
    "col_3" => "update_3",
    "col_4" => "update_4"
];
// 當(dāng)上述上中col_1為主鍵時(shí),value_1,將自動(dòng)當(dāng)WHERE條件,解析為: WHERE `col_1` = value_1,至少要存在一個(gè)主鍵

// 更新
db::save();
// 如果綁定$where條件,則上述主鍵效果將消失
db::$where = [opt...];  // 值的寫法同上

Example-5:Select操作

// join表,綁定操作表與where綁定不再演示
db::$join = ["table_b"=>["table_a.id","table_b.aid","INNER","="]];
// 一維數(shù)組中的鍵名為連接表的表名,二維數(shù)組中前兩個(gè)值為連接條件-必填項(xiàng),第三個(gè)默認(rèn)為INNER,第四個(gè)默認(rèn)為=

db::$join = ["table_b"=>"LEFT JOIN table_b ON table_a.id=table_b.aid"];
//當(dāng)值為一個(gè)字符串時(shí),將直接拼接

// 分組查詢 
db::$group = ["sex","age"];  // 值為要分組的列名字即可
db::$group = "sex"           // 值為字符串時(shí)直接拼接為 GROUP BY sex

// 排序
db::$order = ["id" => "DESC"];  // 關(guān)聯(lián)數(shù)組將解析成 ORDER BY `id` DESC
db::$order = ["id"];              // 索引數(shù)組將解析成 ORDER BY `id` ASC,默認(rèn)為ASC排序
db::$order = "id";              // 字符串將解析成 ORDER BY id 自動(dòng)拼接

// 分頁(yè)
db::$limit = [0,10]  // 存在兩個(gè)值時(shí)將解析為 LIMIT 0,10;
db::$limit = [1]     // 存在一個(gè)值時(shí)將解析為 LIMIT 1;
db::$limit = "0,20"  // 字符串將解析為 LIMIT 0,20;

// 查詢
db::select();  // 多條
db::first();   // 第一條

Example-6:聚合函數(shù)

db::count(); // count

db::sum();   // sum

db::avg();   // avg

db::min();   // min

db::max();   // max

Example-7:附加1

db::begin();              // 開(kāi)啟事務(wù)

db::commit();              // 提交事務(wù)

db::rollBack();          // 回滾事務(wù)

db::setAttr($key, $val); // 設(shè)置PDO屬性

db::dec();                  // 字段自增,需綁定數(shù)據(jù) db::$data = ["age"=>2]該自增步長(zhǎng)為2,省略則默認(rèn)為1

db::inc();                  // 字段自增,需綁定數(shù)據(jù) db::$data = ["age"=>2]該自減步長(zhǎng)為2,省略則默認(rèn)為1

db::do($sql, [$flag]);      //執(zhí)行一條SQL語(yǔ)句,不分查詢還是執(zhí)行,自動(dòng)分配.第二個(gè)參數(shù)同下

db::query($sql,[$flag]); //執(zhí)行一條有結(jié)果集的SQL語(yǔ)句, $flag 默認(rèn)為false,為true是返回一條記錄

db::exec($sql, [$flag]); //執(zhí)行一條無(wú)結(jié)果集的SQL語(yǔ)句, $flag 默認(rèn)為false,為true時(shí)返回insert語(yǔ)句的插入的ID,其他語(yǔ)句無(wú)效

// 執(zhí)行拼接SQL可使用預(yù)處理綁定參數(shù)形式,然后綁定$bind即可
$sql = "UPDATE `tablename` SET col_a=:col_a WHERE id=:id";
db::$bind = [":col_a" => "update_a",":id"=>1];
db::do($sql); //或者 db::exec($sql);

db::clear() // 該方法將重置$data,$field,$where,$order,$group,$limit,$join,$bind數(shù)據(jù),默認(rèn)每執(zhí)行一次語(yǔ)句將自動(dòng)重置

db::init([conf...], $reconnect = false); // conf數(shù)組的值可為數(shù)據(jù)庫(kù)基本配置(host,user,pwd等) $reconnext為true是將重新連接mysql
傳參寫法

Example-8:附加2

// insert
db::inster("tablename",[data...],$where);

// delete
db::del("tablename", $where);

// update
db::save("tablename", [data...], $where);

// select
db::select("tablename",[opt..]);  //opt參數(shù)可傳入field,where,join,order,group,limit等信息

// first
db::first("tablename",[opt..]);   //opt參數(shù)同上

// avg
db::avg("tablename",[opt..]);        //opt參數(shù)同上

// count
db::count("tablename",[opt..]);   //opt參數(shù)同上

// sum
db::sum("tablename",[opt..]);     //opt參數(shù)同上

// min
db::min("tablename",[opt..]);     //opt參數(shù)同上

// max
db::max("tablename",[opt..]);     //opt參數(shù)同上

// dec
db::dec("tablename",[data..],$where);    //參數(shù)說(shuō)明同綁定參數(shù)

// inc
db::inc("tablename",[data..],$where);    //參數(shù)說(shuō)明同綁定參數(shù)
本文出自個(gè)人博客 最好的安排 轉(zhuǎn)載請(qǐng)注明出處!

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/26232.html

相關(guān)文章

  • 寫一個(gè)“特殊”的查詢構(gòu)造器 - (前言)

    摘要:而在項(xiàng)目開(kāi)發(fā)中,我們想要的是一個(gè)更好用的可維護(hù)的工具,此時(shí),對(duì)代碼的封裝模塊化就顯得尤為重要,于是出現(xiàn)了兩種方案查詢構(gòu)造器,對(duì)象關(guān)系映射。典型環(huán)境下按照一般的查詢構(gòu)造器處理就行。 文章目錄 寫一個(gè)特殊的查詢構(gòu)造器 - (前言) 寫一個(gè)特殊的查詢構(gòu)造器 - (一、程序結(jié)構(gòu),基礎(chǔ)封裝) 寫一個(gè)特殊的查詢構(gòu)造器 - (二、第一條語(yǔ)句) 寫一個(gè)特殊的查詢構(gòu)造器 - (三、條件查詢) 寫一個(gè)特殊...

    GitChat 評(píng)論0 收藏0
  • PDO擴(kuò)展PDO::ATTR_AUTOCOMMIT 出坑記

    摘要:?jiǎn)栴}描述我自己以擴(kuò)展為基礎(chǔ),封裝了一個(gè)異步任務(wù)服務(wù)器框架,數(shù)據(jù)庫(kù)操作使用擴(kuò)展,但是在插入數(shù)據(jù)的時(shí)候,出現(xiàn)了異常情況,具體狀況如下表引擎的正常插入,沒(méi)有問(wèn)題表引擎查詢都可以的,但是插入失敗,出現(xiàn)回滾現(xiàn)象,字段變化,表的變化,正常返回解決過(guò)程監(jiān) 問(wèn)題描述 我自己以swoole擴(kuò)展為基礎(chǔ),封裝了一個(gè)異步任務(wù)服務(wù)器框架,數(shù)據(jù)庫(kù)操作使用pdo擴(kuò)展,但是在插入數(shù)據(jù)的時(shí)候,出現(xiàn)了異常情況,具體狀況如...

    Binguner 評(píng)論0 收藏0
  • Laravel核心解讀--Database(一)基礎(chǔ)介紹

    摘要:第三步注冊(cè)工廠啟動(dòng)數(shù)據(jù)庫(kù)服務(wù)數(shù)據(jù)庫(kù)服務(wù)的啟動(dòng)主要設(shè)置的連接分析器,讓能夠用服務(wù)連接數(shù)據(jù)庫(kù)。 在我們學(xué)習(xí)和使用一個(gè)開(kāi)發(fā)框架時(shí),無(wú)論使用什么框架,如何連接數(shù)據(jù)庫(kù)、對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查都是學(xué)習(xí)的重點(diǎn),在Laravel中我們可以通過(guò)兩種方式與數(shù)據(jù)庫(kù)進(jìn)行交互: DB, DB是與PHP底層的PDO直接進(jìn)行交互的,通過(guò)查詢構(gòu)建器提供了一個(gè)方便的接口來(lái)創(chuàng)建及運(yùn)行數(shù)據(jù)庫(kù)查詢語(yǔ)句。 Eloquent...

    reclay 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<