国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 php教程 php手冊(cè) 全新的PDO數(shù)據(jù)庫(kù)操作類php版(僅適用Mysql)

全新的PDO數(shù)據(jù)庫(kù)操作類php版(僅適用Mysql)

Jun 13, 2016 am 11:58 AM
mysql pdo php 程式碼 作者 複製 操作 資料庫(kù) 日期 類別 適用

復(fù)制代碼 代碼如下:


/**
* 作者:胡睿
* 日期:2012/07/21
* 電郵:hooray0905@foxmail.com
*/

class HRDB{
protected $pdo;
protected $res;
protected $config;

/*構(gòu)造函數(shù)*/
function __construct($config){
$this->Config = $config;
$this->connect();
}

/*數(shù)據(jù)庫(kù)連接*/
public function connect(){
$this->pdo = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password']);
$this->pdo->query('set names utf8;');
//把結(jié)果序列化成stdClass
//$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
//自己寫代碼捕獲Exception
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

/*數(shù)據(jù)庫(kù)關(guān)閉*/
public function close(){
$this->pdo = null;
}

public function query($sql){
$res = $this->pdo->query($sql);
if($res){
$this->res = $res;
}
}
public function exec($sql){
$res = $this->pdo->exec($sql);
if($res){
$this->res = $res;
}
}
public function fetchAll(){
return $this->res->fetchAll();
}
public function fetch(){
return $this->res->fetch();
}
public function fetchColumn(){
return $this->res->fetchColumn();
}
public function lastInsertId(){
return $this->res->lastInsertId();
}

/**
* 參數(shù)說明
* int $debug 是否開啟調(diào)試,開啟則輸出sql語句
* 0 不開啟
* 1 開啟
* 2 開啟并終止程序
* int $mode 返回類型
* 0 返回多條記錄
* 1 返回單條記錄
* 2 返回行數(shù)
* string/array $table 數(shù)據(jù)庫(kù)表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數(shù)組模式:
* array('tb_member', 'tb_money')
* string/array $fields 需要查詢的數(shù)據(jù)庫(kù)字段,允許為空,默認(rèn)為查找全部,兩種傳值模式
* 普通模式:
* 'username, password'
* 數(shù)組模式:
* array('username', 'password')
* string/array $sqlwhere 查詢條件,允許為空,兩種傳值模式
* 普通模式:
* 'and type = 1 and username like "%os%"'
* 數(shù)組模式:
* array('type = 1', 'username like "%os%"')
* string $orderby 排序,默認(rèn)為id倒序
*/
public function select($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="tbid desc"){
//參數(shù)處理
if(is_array($table)){
$table = implode(', ', $table);
}
if(is_array($fields)){
$fields = implode(', ', $fields);
}
if(is_array($sqlwhere)){
$sqlwhere = ' and '.implode(' and ', $sqlwhere);
}
//數(shù)據(jù)庫(kù)操作
if($debug === 0){
if($mode === 2){
$this->query("select count(tbid) from $table where 1=1 $sqlwhere");
$return = $this->fetchColumn();
}else if($mode === 1){
$this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
$return = $this->fetch();
}else{
$this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
$return = $this->fetchAll();
}
return $return;
}else{
if($mode === 2){
echo "select count(tbid) from $table where 1=1 $sqlwhere";
}else if($mode === 1){
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
}
else{
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
}
if($debug === 2){
exit;
}
}
}

/**
* 參數(shù)說明
* int $debug 是否開啟調(diào)試,開啟則輸出sql語句
* 0 不開啟
* 1 開啟
* 2 開啟并終止程序
* int $mode 返回類型
* 0 無返回信息
* 1 返回執(zhí)行條目數(shù)
* 2 返回最后一次插入記錄的id
* string/array $table 數(shù)據(jù)庫(kù)表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數(shù)組模式:
* array('tb_member', 'tb_money')
* string/array $set 需要插入的字段及內(nèi)容,兩種傳值模式
* 普通模式:
* 'username = "test", type = 1, dt = now()'
* 數(shù)組模式:
* array('username = "test"', 'type = 1', 'dt = now()')
*/
public function insert($debug, $mode, $table, $set){
//參數(shù)處理
if(is_array($table)){
$table = implode(', ', $table);
}
if(is_array($set)){
$set = implode(', ', $set);
}
//數(shù)據(jù)庫(kù)操作
if($debug === 0){
if($mode === 2){
$this->query("insert into $table set $set");
$return = $this->lastInsertId();
}else if($mode === 1){
$this->exec("insert into $table set $set");
$return = $this->res;
}else{
$this->query("insert into $table set $set");
$return = NULL;
}
return $return;
}else{
echo "insert into $table set $set";
if($debug === 2){
exit;
}
}
}

/**
* 參數(shù)說明
* int $debug 是否開啟調(diào)試,開啟則輸出sql語句
* 0 不開啟
* 1 開啟
* 2 開啟并終止程序
* int $mode 返回類型
* 0 無返回信息
* 1 返回執(zhí)行條目數(shù)
* string $table 數(shù)據(jù)庫(kù)表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數(shù)組模式:
* array('tb_member', 'tb_money')
* string/array $set 需要更新的字段及內(nèi)容,兩種傳值模式
* 普通模式:
* 'username = "test", type = 1, dt = now()'
* 數(shù)組模式:
* array('username = "test"', 'type = 1', 'dt = now()')
* string/array $sqlwhere 修改條件,允許為空,兩種傳值模式
* 普通模式:
* 'and type = 1 and username like "%os%"'
* 數(shù)組模式:
* array('type = 1', 'username like "%os%"')
*/
public function update($debug, $mode, $table, $set, $sqlwhere=""){
//參數(shù)處理
if(is_array($table)){
$table = implode(', ', $table);
}
if(is_array($set)){
$set = implode(', ', $set);
}
if(is_array($sqlwhere)){
$sqlwhere = ' and '.implode(' and ', $sqlwhere);
}
//數(shù)據(jù)庫(kù)操作
if($debug === 0){
if($mode === 1){
$this->exec("update $table set $set where 1=1 $sqlwhere");
$return = $this->res;
}else{
$this->query("update $table set $set where 1=1 $sqlwhere");
$return = NULL;
}
return $return;
}else{
echo "update $table set $set where 1=1 $sqlwhere";
if($debug === 2){
exit;
}
}
}

/**
* 參數(shù)說明
* int $debug 是否開啟調(diào)試,開啟則輸出sql語句
* 0 不開啟
* 1 開啟
* 2 開啟并終止程序
* int $mode 返回類型
* 0 無返回信息
* 1 返回執(zhí)行條目數(shù)
* string $table 數(shù)據(jù)庫(kù)表
* string/array $sqlwhere 刪除條件,允許為空,兩種傳值模式
* 普通模式:
* 'and type = 1 and username like "%os%"'
* 數(shù)組模式:
* array('type = 1', 'username like "%os%"')
*/
public function delete($debug, $mode, $table, $sqlwhere=""){
//參數(shù)處理
if(is_array($sqlwhere)){
$sqlwhere = ' and '.implode(' and ', $sqlwhere);
}
//數(shù)據(jù)庫(kù)操作
if($debug === 0){
if($mode === 1){
$this->exec("delete from $table where 1=1 $sqlwhere");
$return = $this->res;
}else{
$this->query("delete from $table where 1=1 $sqlwhere");
$return = NULL;
}
return $return;
}else{
echo "delete from $table where 1=1 $sqlwhere";
if($debug === 2){
exit;
}
}
}
}


其實(shí)使用上,和之前的相差不大,目的就是為了方便移植。

  本次重寫著重處理了幾個(gè)問題:

 ?、?insert語句太復(fù)雜,fields與values對(duì)應(yīng)容易出現(xiàn)誤差

  我們看下最常見的一句sql插入語句

復(fù)制代碼 代碼如下:

insert into tb_member (username, type, dt) values ('test', 1, now())


  在傳統(tǒng)模式下,fields和values參數(shù)是分開傳入的,但卻要保證兩者參數(shù)傳入的順序一致。這很容易導(dǎo)致順序錯(cuò)亂或者漏傳某個(gè)參數(shù)。

  這次已經(jīng)把問題修改了,采用了mysql獨(dú)有的insert語法,同樣是上面那功能,就可以換成這樣的寫法

復(fù)制代碼 代碼如下:

insert into tb_member set username = "test", type = 1, lastlogindt = now()


  就像update一樣,一目了然。

 ?、?部分參數(shù)可以用數(shù)組代替

  比如這樣一句sql

復(fù)制代碼 代碼如下:

delete from tb_member where 1=1 and tbid = 1 and username = "hooray"


  在原先調(diào)用方法的時(shí)候,需要手動(dòng)拼裝好where條件,這樣操作的成本很高,現(xiàn)在完全可以用這種形式

復(fù)制代碼 代碼如下:


$where = array(
'tbid = 1',
'username = "hooray"'
);
$db->delete(1, 0, 'tb_member', $where);


  條件再多也不會(huì)打亂你的思路。同樣,不僅僅是where參數(shù),update里的set也可以以這種形式(具體可參見完整源碼)

復(fù)制代碼 代碼如下:


$set = array('username = "123"', 'type = 1', 'lastlogindt = now()');
$where = array('tbid = 1');
$db->update(1, 0, 'tb_member', $set, $where);


 ?、?可自定義sql語句

  有時(shí)候,sql過于復(fù)雜,導(dǎo)致無法使用類里提供的方法去組裝sql語句,這時(shí)候就需要一個(gè)功能,就是能直接傳入我已經(jīng)組裝好的sql語句執(zhí)行,并返回信息。現(xiàn)在,這功能也有了

復(fù)制代碼 代碼如下:


$db->query('select username, password from tb_member');
$rs = $db->fetchAll();


  是不是很像pdo原生態(tài)的寫法?

 ?、?支持創(chuàng)建多數(shù)據(jù)庫(kù)連接

  原先的因?yàn)橹皇菙?shù)據(jù)庫(kù)操作方法,所以并不支持多數(shù)據(jù)庫(kù)連接,在實(shí)現(xiàn)上需要復(fù)制出2個(gè)相同的文件,修改部分變量,操作實(shí)屬?gòu)?fù)雜?,F(xiàn)在這問題也解決了。

復(fù)制代碼 代碼如下:


$db_hoorayos_config = array(
'dsn'=>'mysql:host=localhost;dbname=hoorayos',
'name'=>'root',
'password'=>'hooray'
);
$db = new HRDB($db_hoorayos_config);

$db_hoorayos_config2 = array(
'dsn'=>'mysql:host=localhost;dbname=hoorayos2',
'name'=>'root',
'password'=>'hooray'
);
$db2 = new HRDB($db_hoorayos_config2);


  這樣就能同時(shí)創(chuàng)建2個(gè)數(shù)據(jù)庫(kù)連接,方便處理數(shù)據(jù)庫(kù)與數(shù)據(jù)庫(kù)交互的情況。

  大致新功能就是這么多了,整個(gè)代碼并不多,歡迎閱讀了解。下面是我在編寫時(shí)寫的測(cè)試代碼,也一并提供上來,方便大家學(xué)習(xí)。

復(fù)制代碼 代碼如下:


require_once('global.php');
require_once('inc/setting.inc.php');

$db = new HRDB($db_hoorayos_config);

echo '


select測(cè)試
';
echo '普通模式,直接字符串傳入
';
$rs = $db->select(1, 0, 'tb_member', 'username, password', 'and type = 1 and username like "%os%"');
echo '
數(shù)組模式,可傳入數(shù)組
';
$fields = array('username', 'password');
$where = array('type = 1', 'username like "%os%"');
$rs = $db->select(1, 0, 'tb_member', $fields, $where);

echo '
insert測(cè)試
';
echo '普通模式,直接字符串傳入
';
$db->insert(1, 0, 'tb_member', 'username = "test", type = 1, lastlogindt = now()');
echo '
數(shù)組模式,可傳入數(shù)組
';
$set = array('username = "test"', 'type = 1', 'lastlogindt = now()');
$db->insert(1, 0, 'tb_member', $set);

echo '
update測(cè)試
';
echo '普通模式,直接字符串傳入
';
$db->update(1, 0, 'tb_member', 'username = "123", type = 1, lastlogindt = now()', 'and tbid = 7');
echo '
數(shù)組模式,可傳入數(shù)組
';
$set = array('username = "123"', 'type = 1', 'lastlogindt = now()');
$where = array('tbid = 1');
$db->update(1, 0, 'tb_member', $set, $where);

echo '
delete測(cè)試
';
echo '普通模式,直接字符串傳入
';
$db->delete(1, 0, 'tb_member', 'and tbid = 1 and username = "hooray"');
echo '
數(shù)組模式,可傳入數(shù)組
';
$where = array(
'tbid = 1',
'username = "hooray"'
);
$db->delete(1, 0, 'tb_member', $where);

echo '
自定義sql
';
$db->query('select username, password from tb_member');
$rs = $db->fetchAll();
var_dump($rs);

$db->close();

作者:胡尐睿丶
本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

超越燈堆:PHP在現(xiàn)代企業(yè)體系結(jié)構(gòu)中的作用 超越燈堆:PHP在現(xiàn)代企業(yè)體系結(jié)構(gòu)中的作用 Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

PHP中的對(duì)象關(guān)聯(lián)映射(ORM)性能調(diào)整 PHP中的對(duì)象關(guān)聯(lián)映射(ORM)性能調(diào)整 Jul 29, 2025 am 05:00 AM

避免N 1查詢問題,通過提前加載關(guān)聯(lián)數(shù)據(jù)來減少數(shù)據(jù)庫(kù)查詢次數(shù);2.僅選擇所需字段,避免加載完整實(shí)體以節(jié)省內(nèi)存和帶寬;3.合理使用緩存策略,如Doctrine的二級(jí)緩存或Redis緩存高頻查詢結(jié)果;4.優(yōu)化實(shí)體生命週期,定期調(diào)用clear()釋放內(nèi)存以防止內(nèi)存溢出;5.確保數(shù)據(jù)庫(kù)索引存在並分析生成的SQL語句以避免低效查詢;6.在無需跟蹤變更的場(chǎng)景下禁用自動(dòng)變更跟蹤,改用數(shù)組或輕量模式提升性能。正確使用ORM需結(jié)合SQL監(jiān)控、緩存、批量處理和適當(dāng)優(yōu)化,在保持開發(fā)效率的同時(shí)確保應(yīng)用性能。

用PHP和RabbitMQ建造彈性微服務(wù) 用PHP和RabbitMQ建造彈性微服務(wù) Jul 27, 2025 am 04:32 AM

要構(gòu)建彈性的PHP微服務(wù),需使用RabbitMQ實(shí)現(xiàn)異步通信,1.通過消息隊(duì)列解耦服務(wù),避免級(jí)聯(lián)故障;2.配置持久化隊(duì)列、持久化消息、發(fā)布確認(rèn)和手動(dòng)ACK以確??煽啃裕?.使用指數(shù)退避重試、TTL和死信隊(duì)列安全處理失?。?.通過supervisord等工具守護(hù)消費(fèi)者進(jìn)程並啟用心跳機(jī)制保障服務(wù)健康;最終實(shí)現(xiàn)系統(tǒng)在故障中持續(xù)運(yùn)作的能力。

為PHP創(chuàng)建準(zhǔn)備生產(chǎn)的Docker環(huán)境 為PHP創(chuàng)建準(zhǔn)備生產(chǎn)的Docker環(huán)境 Jul 27, 2025 am 04:32 AM

使用正確的PHP基礎(chǔ)鏡像並配置安全、性能優(yōu)化的Docker環(huán)境是實(shí)現(xiàn)生產(chǎn)就緒的關(guān)鍵。 1.選用php:8.3-fpm-alpine作為基礎(chǔ)鏡像以減少攻擊面並提升性能;2.通過自定義php.ini禁用危險(xiǎn)函數(shù)、關(guān)閉錯(cuò)誤顯示並啟用Opcache及JIT以增強(qiáng)安全與性能;3.使用Nginx作為反向代理,限制訪問敏感文件並正確轉(zhuǎn)發(fā)PHP請(qǐng)求至PHP-FPM;4.採(cǎi)用多階段構(gòu)建優(yōu)化鏡像,移除開發(fā)依賴,設(shè)置非root用戶運(yùn)行容器;5.可選Supervisord管理多個(gè)進(jìn)程如cron;6.部署前驗(yàn)證無敏感信息洩

在PHP中構(gòu)建不變的物體,並具有可讀的屬性 在PHP中構(gòu)建不變的物體,並具有可讀的屬性 Jul 30, 2025 am 05:40 AM

ReadonlypropertiesinPHP8.2canonlybeassignedonceintheconstructororatdeclarationandcannotbemodifiedafterward,enforcingimmutabilityatthelanguagelevel.2.Toachievedeepimmutability,wrapmutabletypeslikearraysinArrayObjectorusecustomimmutablecollectionssucha

深入了解PHP的內(nèi)部垃圾收集機(jī)制 深入了解PHP的內(nèi)部垃圾收集機(jī)制 Jul 28, 2025 am 04:44 AM

PHP的垃圾回收機(jī)制基於引用計(jì)數(shù),但循環(huán)引用需靠週期性運(yùn)行的循環(huán)垃圾回收器處理;1.引用計(jì)數(shù)在變量無引用時(shí)立即釋放內(nèi)存;2.循環(huán)引用導(dǎo)致內(nèi)存無法自動(dòng)釋放,需依賴GC檢測(cè)並清理;3.GC在“可能根”zval達(dá)閾值或手動(dòng)調(diào)用gc_collect_cycles()時(shí)觸發(fā);4.長(zhǎng)期運(yùn)行的PHP應(yīng)用應(yīng)監(jiān)控gc_status()、適時(shí)調(diào)用gc_collect_cycles()以避免內(nèi)存洩漏;5.最佳實(shí)踐包括避免循環(huán)引用、使用gc_disable()優(yōu)化性能關(guān)鍵區(qū)及通過ORM的clear()方法解引用對(duì)象,最

無服務(wù)器革命:使用BREF部署可擴(kuò)展的PHP應(yīng)用程序 無服務(wù)器革命:使用BREF部署可擴(kuò)展的PHP應(yīng)用程序 Jul 28, 2025 am 04:39 AM

Bref使PHP開發(fā)者能無需管理服務(wù)器即可構(gòu)建可擴(kuò)展、成本高效的應(yīng)用。 1.Bref通過提供優(yōu)化的PHP運(yùn)行時(shí)層,將PHP帶入AWSLambda,支持PHP8.3等版本,並與Laravel、Symfony等框架無縫集成;2.部署步驟包括:使用Composer安裝Bref,配置serverless.yml定義函數(shù)和事件,如HTTP端點(diǎn)和Artisan命令;3.執(zhí)行serverlessdeploy命令即可完成部署,自動(dòng)配置APIGateway並生成訪問URL;4.針對(duì)Lambda限制,Bref提供解決

python檢查字典中是否存在關(guān)鍵 python檢查字典中是否存在關(guān)鍵 Jul 27, 2025 am 03:08 AM

推薦使用in關(guān)鍵字檢查字典中是否存在某個(gè)鍵,因?yàn)樗?jiǎn)潔、高效且可讀性強(qiáng);2.不推薦使用get()方法判斷鍵是否存在,因?yàn)楫?dāng)鍵存在但值為None時(shí)會(huì)誤判;3.可以使用keys()方法,但多餘,因in默認(rèn)即檢查鍵;4.在需要取值且預(yù)期鍵通常存在時(shí),可用try-except捕獲KeyError異常。最推薦的做法是使用in關(guān)鍵字,既安全又高效,且不受值為None的影響,適合絕大多數(shù)場(chǎng)景。

See all articles