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

首頁(yè) 后端開(kāi)發(fā) php教程 全新的PDO數(shù)據(jù)庫(kù)操作類(lèi)php版(僅適用Mysql)_PHP教程

全新的PDO數(shù)據(jù)庫(kù)操作類(lèi)php版(僅適用Mysql)_PHP教程

Jul 21, 2016 pm 03:17 PM
mysql pdo php 代碼 作者 復(fù)制 操作 數(shù)據(jù)庫(kù) 日期 類(lèi) 適用

復(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);
//自己寫(xiě)代碼捕獲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ù)說(shuō)明
* int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
* 0 不開(kāi)啟
* 1 開(kāi)啟
* 2 開(kāi)啟并終止程序
* int $mode 返回類(lèi)型
* 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ù)說(shuō)明
* int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
* 0 不開(kāi)啟
* 1 開(kāi)啟
* 2 開(kāi)啟并終止程序
* int $mode 返回類(lèi)型
* 0 無(wú)返回信息
* 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ù)說(shuō)明
* int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
* 0 不開(kāi)啟
* 1 開(kāi)啟
* 2 開(kāi)啟并終止程序
* int $mode 返回類(lèi)型
* 0 無(wú)返回信息
* 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ù)說(shuō)明
* int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
* 0 不開(kāi)啟
* 1 開(kāi)啟
* 2 開(kāi)啟并終止程序
* int $mode 返回類(lèi)型
* 0 無(wú)返回信息
* 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í)使用上,和之前的相差不大,目的就是為了方便移植。

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

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

  我們看下最常見(jiàn)的一句sql插入語(yǔ)句

復(fù)制代碼 代碼如下:
insert into tb_member (username, type, dt) values ('test', 1, now())

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

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

復(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也可以以這種形式(具體可參見(jiàn)完整源碼)

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

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

 ?、?可自定義sql語(yǔ)句

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

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

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

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

 ?、?支持創(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)在這問(wè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è)代碼并不多,歡迎閱讀了解。下面是我在編寫(xiě)時(shí)寫(xiě)的測(cè)試代碼,也一并提供上來(lái),方便大家學(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();

作者:胡尐睿丶

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/325810.htmlTechArticle復(fù)制代碼 代碼如下: /** * 作者:胡睿 * 日期:2012/07/21 * 電郵:hooray0905@foxmail.com */ class HRDB{ protected $pdo; protected $res; protected $config; /*構(gòu)造函...
本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系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脫衣機(jī)

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集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

熱門(mén)話題

在C中使用std :: Chrono 在C中使用std :: Chrono Jul 15, 2025 am 01:30 AM

std::chrono在C 中用于處理時(shí)間,包括獲取當(dāng)前時(shí)間、測(cè)量執(zhí)行時(shí)間、操作時(shí)間點(diǎn)與持續(xù)時(shí)間及格式化解析時(shí)間。1.獲取當(dāng)前時(shí)間使用std::chrono::system_clock::now(),可轉(zhuǎn)換為可讀字符串但系統(tǒng)時(shí)鐘可能不單調(diào);2.測(cè)量執(zhí)行時(shí)間應(yīng)使用std::chrono::steady_clock以確保單調(diào)性,并通過(guò)duration_cast轉(zhuǎn)換為毫秒、秒等單位;3.時(shí)間點(diǎn)(time_point)和持續(xù)時(shí)間(duration)可相互操作,但需注意單位兼容性和時(shí)鐘紀(jì)元(epoch)

PHP如何處理環(huán)境變量? PHP如何處理環(huán)境變量? Jul 14, 2025 am 03:01 AM

toAccessenvironmentVariablesInphp,useGetenv()或$ _envsuperglobal.1.getEnv('var_name')retievesSpecificvariable.2。$ _ en v ['var_name'] accessesvariablesifvariables_orderInphp.iniincludes“ e” .setVariablesViaCliWithvar = vualitephpscript.php,inapach

為什么我們?cè)u(píng)論:PHP指南 為什么我們?cè)u(píng)論:PHP指南 Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

為MySQL表中的列選擇適當(dāng)?shù)臄?shù)據(jù)類(lèi)型 為MySQL表中的列選擇適當(dāng)?shù)臄?shù)據(jù)類(lèi)型 Jul 15, 2025 am 02:25 AM

insetTingUpmysqltables,選擇theStherightDatatatPesisionCrucialForeffifeffifeffifeffificeFifeffifeFrifeFifeScalible

PHP檢查字符串是否以特定的字符串開(kāi)頭 PHP檢查字符串是否以特定的字符串開(kāi)頭 Jul 14, 2025 am 02:44 AM

在PHP中判斷字符串是否以特定字符串開(kāi)頭可通過(guò)多種方法實(shí)現(xiàn):1.使用strncmp()比較前n個(gè)字符,若返回0則開(kāi)頭匹配,不區(qū)分大小寫(xiě);2.使用strpos()檢查子字符串位置是否為0,區(qū)分大小寫(xiě),可用stripos()替代實(shí)現(xiàn)不區(qū)分大小寫(xiě);3.可封裝startsWith()或str_starts_with()函數(shù)提高復(fù)用性;此外需注意空字符串默認(rèn)返回true、編碼兼容性及性能差異,strncmp()通常效率更高。

如何避免PHP中未定義的索引錯(cuò)誤 如何避免PHP中未定義的索引錯(cuò)誤 Jul 14, 2025 am 02:51 AM

避免“undefinedindex”錯(cuò)誤的關(guān)鍵方法有三:首先,使用isset()檢查數(shù)組鍵是否存在并確保值不為null,適用于大多數(shù)常規(guī)場(chǎng)景;其次,使用array_key_exists()僅判斷鍵是否存在,適用于需要區(qū)分鍵不存在和值為null的情況;最后,使用空合并運(yùn)算符??(PHP7 )簡(jiǎn)潔地設(shè)置默認(rèn)值,推薦用于現(xiàn)代PHP項(xiàng)目,同時(shí)注意表單字段名拼寫(xiě)、謹(jǐn)慎使用extract()及遍歷前檢查數(shù)組非空以進(jìn)一步規(guī)避風(fēng)險(xiǎn)。

php準(zhǔn)備的語(yǔ)句與條款 php準(zhǔn)備的語(yǔ)句與條款 Jul 14, 2025 am 02:56 AM

使用PHP預(yù)處理語(yǔ)句執(zhí)行帶有IN子句的查詢時(shí),1.需根據(jù)數(shù)組長(zhǎng)度動(dòng)態(tài)生成占位符;2.使用PDO時(shí)可直接傳入數(shù)組,用array_values確保索引連續(xù);3.使用mysqli時(shí)需構(gòu)造類(lèi)型字符串并綁定參數(shù),注意展開(kāi)數(shù)組的方式及版本兼容性;4.避免拼接SQL、處理空數(shù)組和確保數(shù)據(jù)類(lèi)型匹配。具體做法是:先用implode與array_fill生成占位符,再依擴(kuò)展特性綁定參數(shù),從而安全執(zhí)行IN查詢。

如何在Windows上安裝PHP 如何在Windows上安裝PHP Jul 15, 2025 am 02:46 AM

安裝PHP在Windows上的關(guān)鍵步驟包括:1.下載合適的PHP版本并解壓,推薦使用ThreadSafe版本配合Apache或NonThreadSafe版本配合Nginx;2.配置php.ini文件,將php.ini-development或php.ini-production重命名為php.ini;3.將PHP路徑添加到系統(tǒng)環(huán)境變量Path中以便命令行使用;4.測(cè)試PHP是否安裝成功,通過(guò)命令行執(zhí)行php-v和運(yùn)行內(nèi)置服務(wù)器測(cè)試解析能力;5.若使用Apache,需在httpd.conf中配置P

See all articles