<?php
class
Pdodb{
protected
$pdo
;
protected
$res
;
protected
$config
;
function
__construct(
$config
){
$this
->Config =
$config
;
$this
->connect();
}
public
function
connect(){
try
{
$this
->pdo=
new
PDO(
$this
->Config['dsn'],
$this
->Config['username'],
$this
->Config['password']);
$this
->pdo->query(
"set names utf8"
);
}
catch
(Exception
$e
){
echo
'数据库连接失败,详情: ' .
$e
->getMessage () . ' 请在配置文件中数据库连接信息';
exit
();
}
$this
->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
public
function
close(){
$this
->pdo = null;
}
public
function
query(
$sql
,
$return
=false){
$res
=
$this
->pdo->query(
$sql
);
if
(
$res
){
$this
->res =
$res
;
}
if
(
$return
){
return
$res
;
}
}
public
function
exec
(
$sql
,
$return
=false){
$res
=
$this
->pdo->
exec
(
$sql
);
if
(
$res
){
$this
->res =
$res
;
}
if
(
$return
){
return
$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();
}
public
function
lastInsertId2(){
return
$this
->pdo->lastInsertId();
}
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
(
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();
}
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
;
}
}
}
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
);
$set
=implode(
"','"
,
$val
);
}
if
(
$debug
=== 0){
if
(
$mode
=== 2){
$this
->query(
"insert into $table ($sarr) values('"
.
$set
.
"')"
);
}
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
);
}
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
;
}
}
}
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
);
}
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
;
}
}
}
public
function
delete
(
$table
,
$sqlwhere
=
""
,
$debug
=0,
$mode
=0){
if
(
is_array
(
$sqlwhere
)){
$sqlwhere
= '
and
'.implode('
and
',
$sqlwhere
);
}
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
;
}
}
}
}
$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
;
$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
;
$oci_config
=
array
(
'dsn'=>'oci:dbname=orcl',
'username'=>'BAOCRM',
'password'=>'BAOCRM'
);
$oracle
=
new
PDO_DB(
$oci_config
);
$sql
=
"select * from CUSTOMER_LEVEL t"
;
$oracle
->query(
$sql
);
$data
=
$oracle
->fetchAll();
print_r(
$data
);
exit
;
?>