<?php
/**
* PDO封裝類,目的是爲了使用起來更簡單方便
* modify Date: 2014-07-01
*/
class
PDOX {
private
$pdo
= null;
public
$statement
= null;
public
$options
=
array
(
PDO::MYSQL_ATTR_INIT_COMMAND =>
"SET NAMES "
,
);
public
function
__construct(
$dsn
,
$user
=
''
,
$pass
=
''
,
$persistent
= false,
$charset
=
"utf8"
){
$this
->options[PDO::MYSQL_ATTR_INIT_COMMAND] .=
$charset
;
if
(
$persistent
){
$this
->options[PDO::ATTR_PERSISTENT] = true;
}
$this
->pdo =
new
PDO(
$dsn
,
$user
,
$pass
,
$this
->options);
}
/**
全局屬性設置,包括:列名格式和錯誤提示類型 可以使用數字也能直接使用參數
*/
public
function
setAttr(
$param
,
$val
=
''
){
if
(
is_array
(
$param
)){
foreach
(
$param
as
$key
=>
$val
){
$this
->pdo->setAttribute(
$key
,
$val
);
}
}
else
{
if
(
$val
!=
''
){
$this
->pdo->setAttribute(
$param
,
$val
);
}
else
{
return
false;
}
}
}
/**
生成一個編譯好的sql語句模版 你可以使用 ? :name 的形式
返回一個statement對象
*/
public
function
prepare(
$sql
){
if
(
empty
(
$sql
)){
return
false;
}
$this
->statement =
$this
->pdo->prepare(
$sql
);
return
$this
->statement;
}
/**
執行Sql語句,一般用於 增、刪、更新或者設置 返回影響的行數
*/
public
function
exec
(
$sql
){
if
(
empty
(
$sql
)){
return
false;
}
try
{
return
$this
->pdo->
exec
(
$sql
);
}
catch
(Exception
$e
){
return
$e
->getMessage();
}
}
/**
執行有返回值的查詢,返回PDOStatement 可以通過鏈式操作,可以通過這個類封裝的操作獲取數據
*/
public
function
query(
$sql
){
if
(
empty
(
$sql
)){
return
false;
}
$this
->statement =
$this
->pdo->query(
$sql
);
return
$this
->statement;
}
/**
開啓事務
*/
public
function
beginTransaction(){
return
$this
->pdo->beginTransaction();
}
/**
提交事務
*/
public
function
commit(){
return
$this
->pdo->commit();
}
/**
事務回滾
*/
public
function
rollBack(){
return
$this
->pdo->rollBack();
}
public
function
lastInertId(){
return
$this
->pdo->lastInsertId();
}
//** PDOStatement 類操作封裝 **//
/**
讓模版執行SQL語句,1、執行編譯好的 2、在執行時編譯
*/
public
function
execute(
$param
=
""
){
if
(
is_array
(
$param
)){
try
{
return
$this
->statement->execute(
$param
);
}
catch
(Exception
$e
){
//return $this->errorInfo();
return
$e
->getMessage();
}
}
else
{
try
{
return
$this
->statement->execute();
}
catch
(Exception
$e
){
/* 返回的錯誤信息格式
[0] => 42S22
[1] => 1054
[2] => Unknown column 'col' in 'field list'
return $this->errorInfo();
*/
return
$e
->getMessage();
}
}
}
/**
參數1說明:
PDO::FETCH_BOTH
也是默認的,兩者都有(索引,關聯)
PDO::FETCH_ASSOC
關聯數組
PDO::FETCH_NUM
索引
PDO::FETCH_OBJ
對象
PDO::FETCH_LAZY
對象 會附帶queryString查詢SQL語句
PDO::FETCH_BOUND
如果設置了bindColumn,則使用該參數
*/
public
function
fetch(
$fetch_type
= PDO::FETCH_ASSOC){
if
(
is_object
(
$this
->statement)){
return
$this
->statement->fetch(
$fetch_type
);
}
return
false;
}
/**
參數1說明:
PDO::FETCH_BOTH
也是默認的,兩者都有(索引,關聯)
PDO::FETCH_ASSOC
關聯數組
PDO::FETCH_NUM
索引
PDO::FETCH_OBJ
對象
PDO::FETCH_COLUMN
指定列 參數2可以指定要獲取的列
PDO::FETCH_CLASS
指定自己定義的類
PDO::FETCH_FUNC
自定義類 處理返回的數據
PDO_FETCH_BOUND
如果你需要設置bindColumn,則使用該參數
參數2說明:
給定要處理這個結果的類或函數
*/
public
function
fetchAll(
$fetch_type
= PDO::FETCH_ASSOC,
$handle
=
''
){
if
(
empty
(
$handle
)){
return
$this
->statement->fetchAll(
$fetch_type
);
}
return
$this
->statement->fetchAll(
$fetch_type
,
$handle
);
}
/**
以對象形式返回 結果 跟fetch(PDO::FETCH_OBJ)一樣
*/
public
function
fetchObject(
$class_name
){
if
(
empty
(
$clss_name
)){
return
$this
->statement->fetchObject();
}
return
$this
->statement->fetchObject(
$class_name
);
}
public
function
fetchColumn(
$intColumn
= 0){
return
$this
->statement->fetchColumn(
$intColumn
);
}
/**
public function bindColumn($array=array(),$type=EXTR_OVERWRITE){
if(count($array)>0){
extract($array,$type);
}
//$this->statement->bindColumn()
}
*/
/**
以引用的方式綁定變量到佔位符(可以只執行一次prepare,執行多次bindParam達到重複使用的效果)
*/
public
function
bindParam(
$parameter
,
$variable
,
$data_type
=
'STR'
,
$length
= 0){
switch
(
$data_type
){
case
'STR'
:
$data_type
= PDO::PARAM_STR;
break
;
case
'INT'
:
$data_type
= PDO::PARAM_INT;
break
;
default
:
$data_type
=
''
;
break
;
}
return
$this
->statement->bindParam(
$parameter
,
$variable
,
$data_type
,
$length
);
}
/**
返回statement記錄集的行數
*/
public
function
rowCount(){
return
$this
->statement->rowCount();
}
public
function
count
(){
return
$this
->statement->rowCount();
}
public
function
columnCount(){
$this
->statement->execute();
return
$this
->statement->columnCount();
}
public
function
getColumnMeta(
$intColumn
){
return
$this
->statement->getColumnMeta(
$intColumn
);
}
/**
關閉
*/
public
function
close(){
return
$this
->statement->closeCursor();
}
public
function
closeCursor(){
return
$this
->statement->closeCursor();
}
/**
返回錯誤信息也包括錯誤號
*/
private
function
errorInfo(){
return
$this
->statement->errorInfo();
}
/**
返回錯誤號
*/
private
function
errorCode(){
return
$this
->statement->errorCode();
}
//簡化操作
public
function
insert(
$table
,
$data
){
if
(!
is_array
(
$data
)){
return
false;
}
$cols
=
array
();
$vals
=
array
();
foreach
(
$data
as
$key
=>
$val
){
$cols
[] =
$key
;
$vals
[] =
"'"
.
$val
.
"'"
;
}
$sql
=
"INSERT INTO {$table} ("
;
$sql
.= implode(
","
,
$cols
) .
") VALUES ("
;
$sql
.= implode(
","
,
$vals
) .
")"
;
return
$this
->
exec
(
$sql
);
}
public
function
insertBind(
$table
,
$arrayData
){
if
(!
is_array
(
$arrayData
)){
return
false;
}
$vals
=
array_keys
(
$arrayData
);
$cols
=
array
();
/*
$arrayobject = new ArrayObject( $arrayData );
$iterator
= $arrayobject->getIterator();
while($iterator->valid()) {
$vals[] = ':' . $iterator->key() . '';
$iterator->next();
}
*/
$c
= implode(
''
,
$vals
);
$cols
=
array_filter
(
explode
(
':'
,
$c
));
$sql
=
"INSERT INTO {$table} ("
;
$sql
.= implode(
","
,
$cols
) .
") VALUES ("
;
$sql
.= implode(
","
,
$vals
) .
")"
;
$this
->statement =
$this
->pdo->prepare(
$sql
);
$this
->statement->execute(
$arrayData
);
return
$this
->statement->rowCount();
}
public
function
update(
$table
,
$data
,
$where
){
if
(!
is_array
(
$data
)){
return
false;
}
$set
=
array
();
foreach
(
$data
as
$key
=>
$val
){
$set
[] =
$key
.
"='"
.
$val
.
"'"
;
}
$sql
=
"UPDATE {$table} SET "
;
$sql
.= implode(
","
,
$set
);
$sql
.=
" WHERE "
.
$where
;
return
$this
->
exec
(
$sql
);
}
public
function
updateBind(
$sql
,
$arrayWhere
){
if
(
empty
(
$sql
) || !
is_array
(
$arrayWhere
)){
return
false;
}
$this
->statement =
$this
->pdo->prepare(
$sql
);
$this
->statement->execute(
$arrayWhere
);
return
$this
->statement->rowCount();
}
public
function
delete
(
$table
,
$where
){
if
(
empty
(
$table
) ||
empty
(
$where
)){
return
false;
}
$sql
=
"DELETE FROM {$table} WHERE "
.
$where
;
return
$this
->
exec
(
$sql
);
}
public
function
deleteBind(
$sql
,
$arrayWhere
){
if
(
empty
(
$sql
) || !
is_array
(
$arrayWhere
)){
return
false;
}
$this
->statement =
$this
->pdo->prepare(
$sql
);
$this
->statement->execute(
$arrayWhere
);
return
$this
->statement->rowCount();
}
}
?>