http://www.php.cn/php-weizijiaocheng-404645.htmlphp
<?php
html
class
Pdodb{
mysql
protected
$pdo
;
sql
protected
$res
;
數據庫
protected
$config
;
數組
/*構造函數*/
服務器
function
__construct(
$config
){
oracle
$this
->Config =
$config
;
函數
$this
->connect();
sqlserver
}
/*數據庫鏈接*/
public
function
connect(){
try
{
$this
->pdo=
new
PDO(
$this
->Config[
'dsn'
],
$this
->Config[
'username'
],
$this
->Config[
'password'
]);
//$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$this
->pdo->query(
"set names utf8"
);
}
catch
(Exception
$e
){
echo
'數據庫鏈接失敗,詳情: '
.
$e
->getMessage () .
' 請在配置文件中數據庫鏈接信息'
;
exit
();
}
/*
if($this->Config['type']=='oracle'){
$this->pdo->query("set names {$this->Config['charset']};");
}else{
$this->pdo->query("set names {$this->Config['charset']};");
}
*/
//把結果序列化成stdClass
//$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
//本身寫代碼捕獲Exception
//$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this
->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
//屬性名 屬性值 數組以關聯數組返回
}
/*數據庫關閉*/
public
function
close(){
$this
->pdo = null;
}
//用於有記錄結果返回的操做,特別是SELECT操做
public
function
query(
$sql
,
$return
=false){
$res
=
$this
->pdo->query(
$sql
);
if
(
$res
){
$this
->res =
$res
;
// 未返回 return $this->res;
}
if
(
$return
){
return
$res
;
}
}
//主要是針對沒有結果集合返回的操做,好比INSERT、UPDATE、DELETE等操做
public
function
exec
(
$sql
,
$return
=false){
$res
=
$this
->pdo->
exec
(
$sql
);
if
(
$res
){
$this
->res =
$res
;
}
if
(
$return
){
//返回操做是否成功 成功返回1 失敗0
return
$res
;
}
}
//將$this->res以數組返回(所有返回)
public
function
fetchAll(){
return
$this
->res->fetchAll();
}
//將$this->res以數組返回(一條記錄)
public
function
fetch(){
return
$this
->res->fetch();
}
//返回全部字段
public
function
fetchColumn(){
return
$this
->res->fetchColumn();
}
//返回最後插入的id
public
function
lastInsertId(){
return
$this
->res->lastInsertId();
}
//返回最後插入的id
public
function
lastInsertId2(){
return
$this
->pdo->lastInsertId();
}
/**
* 參數說明
* string/array $table 數據庫表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數組模式:
* array('tb_member', 'tb_money')
* string/array $fields 須要查詢的數據庫字段,容許爲空,默認爲查找所有,兩種傳值模式
* 普通模式:
* 'username, password'
* 數組模式:
* array('username', 'password')
* string/array $sqlwhere 查詢條件,容許爲空,兩種傳值模式
* 普通模式(必須加上and,$sqlwhere爲空 1=1 正常查詢):
* 'and type = 1 and username like "%os%"'
* 數組模式:
* array('type = 1', 'username like "%os%"')
* string $orderby 排序,默認爲id倒序
*int $debug 是否開啓調試,開啓則輸出sql語句
* 0 不開啓
* 1 開啓
* 2 開啓並終止程序
* int $mode 返回類型
* 0 返回多條記錄
* 1 返回單條記錄
* 2 返回行數
*/
public
function
select(
$table
,
$fields
=
"*"
,
$sqlwhere
=
""
,
$orderby
=
""
,
$debug
=0,
$mode
=0){
//參數處理
if
(
is_array
(
$table
)){
$table
= implode(
', '
,
$table
);
}
if
(
is_array
(
$fields
)){
$fields
= implode(
','
,
$fields
);
/*
if($this->Config['type']=='oracle'){
//$fields = implode(',',$fields);//CUSTOMER_ID,FIRST_NAME,LAST_NAME,EMAIL
//$fields = implode(",'UTF8','ZHS16GBK') ,convert(",$fields);
//$fields="convert(".$fields.",'UTF8','ZHS16GBK')";
}else{
$fields = implode(',',$fields);
}
*/
}
if
(
is_array
(
$sqlwhere
)){
$sqlwhere
=
' and '
.implode(
' and '
,
$sqlwhere
);
}
//數據庫操做
if
(
$debug
=== 0){
if
(
$mode
=== 2){
//統計
$this
->query(
"select count(*) from $table where 1=1 $sqlwhere"
);
$return
=
$this
->fetchColumn();
}
else
if
(
$mode
=== 1){
//返回一條
$this
->query(
"select $fields from $table where 1=1 $sqlwhere $orderby"
);
$return
=
$this
->fetch();
}
else
{
$this
->query(
"select $fields from $table where 1=1 $sqlwhere $orderby"
);
$return
=
$this
->fetchAll();
//若是 $this->res爲空即sql語句錯誤 會提示Call to a member function fetchAll() on a non-object
}
return
$return
;
}
else
{
if
(
$mode
=== 2){
echo
"select count(*) from $table where 1=1 $sqlwhere"
;
}
else
if
(
$mode
=== 1){
echo
"select $fields from $table where 1=1 $sqlwhere $orderby"
;
}
else
{
echo
"select $fields from $table where 1=1 $sqlwhere $orderby"
;
}
if
(
$debug
=== 2){
exit
;
}
}
}
/**
* 參數說明
* string/array $table 數據庫表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數組模式:
* array('tb_member', 'tb_money')
* string/array $set 須要插入的字段及內容,兩種傳值模式
* 普通模式:
* 'username = "test", type = 1, dt = now()'
* 數組模式:
* array('username = "test"', 'type = 1', 'dt = now()')
* int $debug 是否開啓調試,開啓則輸出sql語句
* 0 不開啓
* 1 開啓
* 2 開啓並終止程序
* int $mode 返回類型
* 0 無返回信息
* 1 返回執行條目數
* 2 返回最後一次插入記錄的id
*/
public
function
oic_insert(
$table
,
$set
,
$debug
=0,
$mode
=0){
//參數處理
if
(
is_array
(
$table
)){
$table
= implode(
', '
,
$table
);
}
if
(
is_array
(
$set
)){
$s
=
''
;
$i
=0;
foreach
(
$set
as
$k
=>
$v
){
$i
++;
$s
[
$i
]=
$k
;
//,鏈接
$val
[
$i
]=
$v
;
}
$sarr
=implode(
","
,
$s
);
//去掉最後一個,
//array_pop($sarr);
$set
=implode(
"','"
,
$val
);
////15221579236','張三','','2001','8','4','女','是
//$set = implode(', ', $set);
}
//數據庫操做
if
(
$debug
=== 0){
if
(
$mode
=== 2){
$this
->query(
"insert into $table ($sarr) values('"
.
$set
.
"')"
);
//$return = $this->lastInsertId();
}
else
if
(
$mode
=== 1){
$this
->
exec
(
"insert into $table ($sarr) values('"
.
$set
.
"')"
);
$return
=
$this
->res;
}
else
{
$this
->query(
"insert into $table ($sarr) values('"
.
$set
.
"')"
);
$return
= NULL;
}
return
$return
;
}
else
{
echo
"insert into $table ($sarr) values('"
.
$set
.
"')"
;
if
(
$debug
=== 2){
exit
;
}
}
}
public
function
insert(
$table
,
$set
,
$debug
=0,
$mode
=0){
//參數處理
if
(
is_array
(
$table
)){
$table
= implode(
', '
,
$table
);
}
if
(
is_array
(
$set
)){
$s
=
''
;
foreach
(
$set
as
$k
=>
$v
){
$s
.=
$k
.
"='"
.
$v
.
"',"
;
//,鏈接
}
$sarr
=
explode
(
','
,
$s
);
//去掉最後一個,
array_pop
(
$sarr
);
$set
=implode(
','
,
$sarr
);
//$set = implode(', ', $set);
}
//數據庫操做
if
(
$debug
=== 0){
if
(
$mode
=== 2){
$this
->query(
"insert into $table set $set"
);
$return
=
$this
->pdo->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
;
}
}
}
/**
* 參數說明
* string $table 數據庫表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數組模式:
* array('tb_member', 'tb_money')
* string/array $set 須要更新的字段及內容,兩種傳值模式
* 普通模式:
* 'username = "test", type = 1, dt = now()'
* 數組模式:
* array('username = "test"', 'type = 1', 'dt = now()')
* string/array $sqlwhere 修改條件,容許爲空,兩種傳值模式
* 普通模式:
* 'and type = 1 and username like "%os%"'
* 數組模式:
* array('type = 1', 'username like "%os%"')
* int $debug 是否開啓調試,開啓則輸出sql語句
* 0 不開啓
* 1 開啓
* 2 開啓並終止程序
* int $mode 返回類型
* 0 無返回信息
* 1 返回執行條目數
*/
public
function
update(
$table
,
$set
,
$sqlwhere
=
""
,
$debug
=0,
$mode
=0){
//參數處理
if
(
is_array
(
$table
)){
$table
= implode(
', '
,
$table
);
}
if
(
is_array
(
$set
)){
$s
=
''
;
foreach
(
$set
as
$k
=>
$v
){
$s
.=
$k
.
"='"
.
$v
.
"',"
;
}
$sarr
=
explode
(
','
,
$s
);
//去掉最後一個,
array_pop
(
$sarr
);
$set
=implode(
','
,
$sarr
);
//$set = implode(', ', $set);
}
if
(
is_array
(
$sqlwhere
)){
$sqlwhere
=
' and '
.implode(
' and '
,
$sqlwhere
);
}
//數據庫操做
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
= true;
}
return
$return
;
}
else
{
echo
"update $table set $set where 1=1 $sqlwhere"
;
if
(
$debug
=== 2){
exit
;
}
}
}
/**
* 參數說明
* string $table 數據庫表
* string/array $sqlwhere 刪除條件,容許爲空,兩種傳值模式
* 普通模式:
* 'and type = 1 and username like "%os%"'
* 數組模式:
* array('type = 1', 'username like "%os%"')
* int $debug 是否開啓調試,開啓則輸出sql語句
* 0 不開啓
* 1 開啓
* 2 開啓並終止程序
* int $mode 返回類型
* 0 無返回信息
* 1 返回執行條目數
*/
public
function
delete
(
$table
,
$sqlwhere
=
""
,
$debug
=0,
$mode
=0){
//參數處理
if
(
is_array
(
$sqlwhere
)){
$sqlwhere
=
' and '
.implode(
' and '
,
$sqlwhere
);
//是字符串需本身加上and
}
//數據庫操做
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
;
}
}
}
}
/*
sqlserver 配置 extension=php_pdo_mssql.dll和extension=php_pdo_sqlsrv.dll 安裝對應的 ntwdblib.dll
http://msdn.microsoft.com/en-us/library/cc296170.aspx 下載php版本對應的sqlsrv擴展
sqlserver 配置 odbc鏈接需開啓extension=php_pdo_odbc.dll
*/
$mssql2008_config
=
array
(
'dsn'
=>
'odbc:Driver={SQL Server};Server=192.168.1.60;Database=his'
,
//數據庫服務器地址
'username'
=>
'sa'
,
'password'
=>
'xxxxx'
,
);
$mssql
=
new
Pdodb(
$mssql2008_config
);
$sql
="select * from
(
select row_number()over(order by tempcolumn)temprownumber,*
from (
select top 10 tempcolumn=0,a.*
from DA_GR_HBFS a
where 1=1
) t
) tt
where temprownumber>0";
$mssql
->query(
$sql
);
while
(
$res
=
$mssql
->fetch()){
$data
[]=
$res
;
}
print_r(
$data
);
exit
;
//mysql 操做
$msyql_config
=
array
(
'dsn'
=>
'mysql:host=localhost;dbname=talk'
,
'username'
=>
'root'
,
'password'
=>
'123456'
);
$mysql
=
new
PDO_DB(
$msyql_config
);
$sql
=
'SELECT user_id, user_name, nickname FROM et_users '
;
$mysql
->query(
$sql
);
$data
=
$mysql
->fetchAll();
print_r(
$data
);
exit
;
//oracle 操做
$oci_config
=
array
(
'dsn'
=>
'oci:dbname=orcl'
,
'username'
=>
'BAOCRM'
,
'password'
=>
'BAOCRM'
);
$oracle
=
new
PDO_DB(
$oci_config
);
//print_r($oracle);exit;//PDO_DB Object ( [pdo:protected] => PDO Object ( ) [res:protected] => [config:protected] => [Config] => Array ( [dsn] => oci:dbname=orcl [name] => PWACRM [password] => PWACRM ) )
$sql
=
"select * from CUSTOMER_LEVEL t"
;
$oracle
->query(
$sql
);
$data
=
$oracle
->fetchAll();
print_r(
$data
);
exit
;
/*
Array
(
[0] => Array
(
[LEVEL_ID] => 1
[0] => 1
[LEVEL_NAME] => 普通會員
[1] => 普通會員
[LEVEL_DETAIL] => 普通會員
[2] => 普通會員
[SORT_NUMBER] => 15
[3] => 15
[CREATE_TIME] => 12-7月 -12
[4] => 12-7月 -12
[CREATE_BY] => 1
[5] => 1
[UPDATE_TIME] => 12-7月 -12
[6] => 12-7月 -12
[UPDATE_BY] => 1
[7] => 1
[STATE] => 正常
[8] => 正常
)
)*/
?>