PHP 基于pdo的数据库操作类
http://www.php.cn/php-weizijiaocheng-404645.html
<?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'
]);
//$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] => 正常
)
)*/
?>
PHP 基于pdo的数据库操作类的更多相关文章
- 一个基于PDO的数据库操作类(新) 一个PDO事务实例
<?php /* * 作者:胡睿 * 日期:2011/03/19 * 电邮:hooray0905@foxmail.com * * 20110319 * 常用数据库操作,如:增删改查,获取单条记录 ...
- php pdo mysql数据库操作类
<?php namespace iphp\core; use iphp\App; /** * 数据库操作基类 基于pdo * @author xuen * 支持链式操作,支持参数绑定 * 说明1 ...
- PDO数据库操作类
<?php include 'common_config.php'; /** * Class Mysql * PDO数据库操作类 */ class Mysql { protected stati ...
- C#全能数据库操作类及调用示例
C#全能数据库操作类及调用示例 using System; using System.Data; using System.Data.Common; using System.Configuratio ...
- 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...
- Android打造属于自己的数据库操作类。
1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...
- PHP 数据库操作类:ezSQL
EZSQL类介绍: 下载地址:http://www.jb51.net/codes/26393.htmlezsql是一个小型的快速的数据库操作类,可以让你很容易地用PHP操作各种数据库( MySQL.o ...
- 通用数据库操作类,前端easyui-datagrid,form
实现功能: 左端datagrid显示简略信息,右侧显示选中行详细信息,数据库增删改 (1)点击选中行,右侧显示详细信息,其中[新增].[修改].[删除]按钮可用,[保存]按钮禁用 (2)点击[ ...
- php : mysql数据库操作类演示
设计目标: 1,该类一实例化,就可以自动连接上mysql数据库: 2,该类可以单独去设定要使用的连接编码(set names XXX) 3,该类可以单独去设定要使用的数据库(use XXX): 4,可 ...
随机推荐
- D3比例尺
D3中有个重要的概念就是比例尺.比例尺就是把一组输入域映射到输出域的函数.映射就是两个数据集之间元素相互对应的关系.比如输入是1,输出是100,输入是5,输出是10000,那么这其中的映射关系就是你所 ...
- 推荐一个好用的E2E前端测试框架cypress
Cypress 是一个E2E的前端自动化测试框架,同样是基于BDD的思想设计的,话不多说,上demo https://github.com/Spillage/cypress-demo PS, 还有一个 ...
- s6k0:一种输入法分词关联模型演示
实现:用kotlin.但是考虑到习惯问题,需要借助akka实现erlang的actor,以及rx.java 需求:略 预计:最快两周 保守估计时间:2019年3月左右 优先级:低 加速方法:打饭钱 赞 ...
- 使用Python+turtle绘制动画重现龟兔赛跑现场
问题描述: 在经典的龟兔赛跑故事中,兔子本来是遥遥领先的,结果因为骄傲,居然在比赛现场睡了一觉,醒来后发现乌龟已经快到终点了,于是赶紧追赶,无奈为时已晚,最终输掉了比赛.本文使用turtle绘制乌龟和 ...
- Python_Mix*生成器,生成器函数,推导式,生成器表达式
生成器: 生成器的本质就是迭代器 生成器一般由生成器函数或者生成器表达式来创建,其实就是手写的迭代器 def func(): print('abc') yield 222 #由于函数中有了yield ...
- 使用 spring封装的javamail linux服务器发送邮件失败解决
原文参考:https://blog.csdn.net/a540891049/article/details/79385471 由于某些平台的linxu服务器为了安全起见 屏蔽了发送邮件的常用端口 25 ...
- Double H
##Double H Team 1.队员 王熙航211606379(队长) 李冠锐211606364 曾磊鑫211606350 戴俊涵211606359 聂寒冰211606324 杨艺勇2116063 ...
- LeetCode算法历程-01
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- C++中:(*p)++和*(p++)和*p++的区别
1. 理解自增自减运算符 (1)后置自增自减运算符优先级>前置自增自减运算符. (2)前置自增自减运算符“变量先自增自减再使用”. (3)后置自增自减运算符“变量先使用再自增自减”. ...
- xenserver添加磁盘后挂载为本地存储库并且删除
方法一: 1.1:查看磁盘列表 fdisk -l [root@xenserver ~]# fdisk -l Disk /dev/sdb: 7999.4 GB, 7999376588800 bytes, ...