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

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

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

Jun 13, 2016 am 11:58 AM
mysql pdo php 代碼 作者 復(fù)制 操作 數(shù)據(jù)庫 日期 適用

復(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ù)庫連接*/
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ù)庫關(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ù)庫表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數(shù)組模式:
* array('tb_member', 'tb_money')
* string/array $fields 需要查詢的數(shù)據(jù)庫字段,允許為空,默認(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ù)庫操作
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ù)庫表,兩種傳值模式
* 普通模式:
* '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ù)庫操作
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ù)庫表,兩種傳值模式
* 普通模式:
* '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ù)庫操作
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ù)庫表
* 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ù)庫操作
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;
}
}
}
}


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

  本次重寫著重處理了幾個問題:

  ① insert語句太復(fù)雜,fields與values對應(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)致順序錯亂或者漏傳某個參數(shù)。

  這次已經(jīng)把問題修改了,采用了mysql獨有的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)用方法的時候,需要手動拼裝好where條件,這樣操作的成本很高,現(xiàn)在完全可以用這種形式

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


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


  條件再多也不會打亂你的思路。同樣,不僅僅是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語句

  有時候,sql過于復(fù)雜,導(dǎo)致無法使用類里提供的方法去組裝sql語句,這時候就需要一個功能,就是能直接傳入我已經(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ù)庫連接

  原先的因為只是數(shù)據(jù)庫操作方法,所以并不支持多數(shù)據(jù)庫連接,在實現(xiàn)上需要復(fù)制出2個相同的文件,修改部分變量,操作實屬復(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);


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

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

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


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

$db = new HRDB($db_hoorayos_config);

echo '


select測試
';
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測試
';
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測試
';
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測試
';
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();

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

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

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

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

將語義結(jié)構(gòu)應(yīng)用于html的文章,部分和旁邊 將語義結(jié)構(gòu)應(yīng)用于html的文章,部分和旁邊 Jul 05, 2025 am 02:03 AM

在HTML中合理使用語義化標(biāo)簽?zāi)芴嵘撁娼Y(jié)構(gòu)清晰度、可訪問性和SEO效果。1.用于獨立內(nèi)容區(qū)塊,如博客文章或評論,需保持自包含性;2.用于歸類相關(guān)內(nèi)容,通常包含標(biāo)題,適用于頁面不同模塊;3.用于與主內(nèi)容相關(guān)但非核心的輔助信息,如側(cè)邊欄推薦或作者簡介。實際開發(fā)中應(yīng)結(jié)合、等標(biāo)簽,避免過度嵌套,保持結(jié)構(gòu)簡潔,并通過開發(fā)者工具驗證結(jié)構(gòu)合理性。

在MySQL列和查詢中處理零值 在MySQL列和查詢中處理零值 Jul 05, 2025 am 02:46 AM

處理MySQL中的NULL值需注意:1.設(shè)計表時關(guān)鍵字段設(shè)為NOTNULL,可選字段允許NULL;2.查詢判斷必須用ISNULL或ISNOTNULL,不能用=或!=;3.可用IFNULL或COALESCE函數(shù)替換顯示默認(rèn)值;4.插入或更新時直接使用NULL值需謹(jǐn)慎,注意數(shù)據(jù)源和ORM框架處理方式。NULL表示未知值,不等于任何值,包括自身,因此查詢、統(tǒng)計、連接表時要特別小心,避免漏數(shù)據(jù)或邏輯錯誤。合理使用函數(shù)和約束可以有效減少因NULL帶來的干擾。

分析MySQL緩慢查詢?nèi)罩疽圆檎倚阅芷款i 分析MySQL緩慢查詢?nèi)罩疽圆檎倚阅芷款i Jul 04, 2025 am 02:46 AM

開啟MySQL慢查詢?nèi)罩静⒎治隹啥ㄎ恍阅軉栴}。 1.編輯配置文件或動態(tài)設(shè)置slow_query_log和long_query_time;2.日志包含Query_time、Lock_time、Rows_examined等關(guān)鍵字段,輔助判斷效率瓶頸;3.使用mysqldumpslow或pt-query-digest工具高效分析日志;4.優(yōu)化建議包括添加索引、避免SELECT*、拆分復(fù)雜查詢等。例如為user_id加索引能顯著減少掃描行數(shù),提升查詢效率。

使用mySQL中的mysqldump執(zhí)行邏輯備份 使用mySQL中的mysqldump執(zhí)行邏輯備份 Jul 06, 2025 am 02:55 AM

mysqldump是用于執(zhí)行MySQL數(shù)據(jù)庫邏輯備份的常用工具,它生成包含CREATE和INSERT語句的SQL文件以重建數(shù)據(jù)庫。1.它不備份原始文件,而是將數(shù)據(jù)庫結(jié)構(gòu)和內(nèi)容轉(zhuǎn)換為可移植的SQL命令;2.適用于小型數(shù)據(jù)庫或選擇性恢復(fù),不適合TB級數(shù)據(jù)快速恢復(fù);3.常用選項包括--single-transaction、--databases、--all-databases、--routines等;4.恢復(fù)時使用mysql命令導(dǎo)入,并可關(guān)閉外鍵檢查以提升速度;5.建議定期測試備份、使用壓縮、自動化調(diào)

建立與MySQL Server的安全遠(yuǎn)程連接 建立與MySQL Server的安全遠(yuǎn)程連接 Jul 04, 2025 am 01:44 AM

TosecurelyConnectToaremoteMysqlServer,Usesshtunneling,configuremysqlforremoteaccess,setFireWallrules,andConsidersSlencryption 。首先,stardansshtunnelwithssh-l3307:localhost:3306user@remote-Server-server-nandConnectViamySql-h127.0.0.0.0.1-p3307.second,editmys

在MySQL中以極限和偏移的限制結(jié)果 在MySQL中以極限和偏移的限制結(jié)果 Jul 05, 2025 am 02:41 AM

MySQL分頁常用LIMIT和OFFSET實現(xiàn),但大數(shù)據(jù)量下性能較差。1.LIMIT控制每頁數(shù)量,OFFSET控制起始位置,語法為LIMITNOFFSETM;2.性能問題源于OFFSET掃描過多記錄并丟棄,導(dǎo)致效率低;3.優(yōu)化建議包括使用游標(biāo)分頁、索引加速、懶加載;4.游標(biāo)分頁通過上一頁最后一條記錄的唯一值定位下一頁起點,避免OFFSET,適合“下一頁”操作,不適合隨機跳轉(zhuǎn)。

通過MySQL中的群組和有條款匯總數(shù)據(jù) 通過MySQL中的群組和有條款匯總數(shù)據(jù) Jul 05, 2025 am 02:42 AM

GROUPBY用于按字段分組數(shù)據(jù)并執(zhí)行聚合操作,HAVING用于過濾分組后的結(jié)果。例如,使用GROUPBYcustomer_id可計算每個客戶的總消費金額;配合HAVING可篩選出總消費超過1000的客戶。SELECT后的非聚合字段必須出現(xiàn)在GROUPBY中,HAVING可使用別名或原始表達(dá)式進(jìn)行條件篩選。常見技巧包括統(tǒng)計每組數(shù)量、多字段分組、結(jié)合多個條件過濾。

管理MySQL中的交易和鎖定行為 管理MySQL中的交易和鎖定行為 Jul 04, 2025 am 02:24 AM

MySQL事務(wù)和鎖機制是并發(fā)控制和性能調(diào)優(yōu)的關(guān)鍵。1.使用事務(wù)時,務(wù)必顯式開啟并保持事務(wù)短小,避免長事務(wù)導(dǎo)致資源占用和undolog膨脹;2.加鎖操作包括共享鎖和排他鎖,SELECT...FORUPDATE加X鎖,SELECT...LOCKINSHAREMODE加S鎖,寫操作自動加鎖,應(yīng)使用索引減少鎖粒度;3.隔離級別默認(rèn)為可重復(fù)讀,適用于大多數(shù)場景,修改需謹(jǐn)慎;4.死鎖排查可通過SHOWENGINEINNODBSTATUS命令分析最近一次死鎖詳情,優(yōu)化方式包括統(tǒng)一執(zhí)行順序、增加索引、引入隊列系

See all articles