PHP封装的PDO数据库操作类实例
作者:北京流浪儿 时间:2023-11-18 04:54:31
本文实例讲述了PHP封装的PDO数据库操作类。分享给大家供大家参考,具体如下:
<?php
class DatabaseHandler {
/**
* sql语句查询
*/
public static function query_data ($dataName,$sql,$query=array()) {
$result = array();
if (!empty($sql)) {
$data = Bj_PdoDB::factory($dataName)->allPrepare($sql, $query);
if (!$data->isEmpty()) {
$result = $data->data;
}
}
return $result;
}
/**
* 查询数据
*/
public static function select_data ($dataName,$tableName,$where,$param=array(),$desc='') {
$result = array();
$query = array();
$query_str = '1=1';
$tmp = '';
$str_arr = '';
if (!empty($param)) {
$str = '';
foreach ($param as $val) {
$str .= '`'.$val.'`,';
}
$str = substr($str,0,-1);
}
$str_arr = !empty($str) ? $str : '*';
if ( !empty($where) ) {
foreach ($where as $key => $value) {
$tmp .= ' AND '.$key.'=:'.$key.'';
$query[':'.$key] = $value;
}
}
$query_str .= $tmp;
if (!empty($desc)) {
$query_str .= $desc;
}
$sql = "SELECT {$str_arr} FROM `".$tableName."` WHERE {$query_str} ";
$query_data = Bj_PdoDB::factory($dataName)->allPrepare($sql, $query);
if ( !$query_data->isEmpty() ) {
$result = $query_data->data;
}
return $result;
}
/**
* 批量添加信息
*/
public static function insert_data($dataName,$tableName,$add_param){
$flag = false;
if (!empty($add_param)) {
$fileds = array();
$str = '';
foreach($add_param as $k=>$val){
$str .= '(';
$i = 0;
foreach ($val as $key=>$vl) {
$fileds[$i] = '`'.$key.'`';
$str .= "'".addslashes($vl)."',";
$i++;
}
$str = substr($str,0,-1);
$str .= '),';
}
$filed_str = implode(',',$fileds);
$val_str = substr($str,0,-1);
$sql = "INSERT INTO `".$tableName."` ({$filed_str}) VALUES {$val_str}";
$ret = Bj_PdoDB::factory($dataName)->insertPrepare($sql);
if ( !$ret->isEmpty() ) {
$insertId = $ret->data;
if($insertId > 0){
return $insertId;
}
}
}
return $flag;
}
/**
* 删除数据
*/
public static function delete_data ($dataName,$tableName,$id) {
$query = array(':id' => $id);
$sql = "DELETE FROM ". $tableName. " WHERE `id`=:id";
$db_res = Bj_PdoDB::factory($dataName)->simplePrepare($sql,$query);
return $db_res->data;
}
/**
* 更新数据
*/
public static function update_data ($dataName,$tableName,$id,$array) {
$flag = false;
if ( !empty($array) && !empty($id)) {
$query = array(':id' => $id);
$up_str = '';
foreach($array as $key=>$val) {
$query[':'.$key] = addslashes($val);
$up_str .= '`'.$key.'`=:'.$key.',';
}
$up_str = substr($up_str,0,-1);
$sql = "UPDATE `".$tableName."` SET ".$up_str." WHERE `id`=:id";
$db_res = Bj_PdoDB::factory($dataName)->changePrepare($sql, $query);
$flag = true;
}
return $flag;
}
/**
* 事务
*/
public static function transactionHandler($dataName,$type){
switch ($type) {
case 'begin':
return Bj_PdoDB::factory($dataName)->beginTransaction();
break;
case 'commit':
return Bj_PdoDb::factory($dataName)->commit();
break;
case 'rollback':
return Bj_PdoDb::factory($dataName)->rollback();
break;
default :
exit;
}
}
/**
* curl获取数据get
*/
public static function curl_data ($curl_url){
if (!empty($curl_url)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
return false;
}
/**
* curl提交数据
*/
public static function curl_data_post ($ch_url,$param) {
$result = array();
if (!empty($ch_url) && !empty($param)) {
$query_string = http_build_query($param);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ch_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded; charset=UTF-8"));
$output = curl_exec($ch);
curl_close($ch);
$result = !empty($output) ? json_decode($output,true) : array();
}
return $result;
}
}
希望本文所述对大家PHP程序设计有所帮助。
标签:PHP,PDO,数据库
0
投稿
猜你喜欢
Go爬虫(http、goquery和colly)详解
2024-05-08 10:52:43
Matplotlib绘制子图的常见几种方法
2022-04-04 06:19:47
git 优雅的撤销中间某次提交方法
2023-12-27 11:12:50
python 从文件夹抽取图片另存的方法
2022-08-26 13:00:55
Django中自定义查询对象的具体使用
2021-05-26 03:11:18
Django中实现一个高性能计数器(Counter)实例
2023-11-16 03:53:48
JavaScript数组合并的8种常见方法小结
2024-04-16 09:32:55
django数据模型on_delete, db_constraint的使用详解
2023-02-16 04:48:06
vue中的input框点击后不聚焦问题
2024-05-02 16:33:12
细化解析:MySQL 搜索中的大小写敏感性
2008-11-27 15:53:00
Python 多进程并发操作中进程池Pool的实例
2022-06-28 16:31:37
采用python实现简单QQ单用户机器人的方法
2022-06-26 03:28:40
使用python实现mqtt的发布和订阅
2021-04-16 12:27:53
SQL Server查询结果导出到EXCEL表格的图文教程
2024-01-23 10:40:22
Python基于回溯法子集树模板解决取物搭配问题实例
2023-11-20 04:46:53
python3 线性回归验证方法
2022-12-03 10:49:09
python 如何将数据写入本地txt文本文件的实现方法
2021-05-02 14:11:52
详解python3 GUI刷屏器(附源码)
2022-02-02 12:34:15
Python 数据结构之队列的实现
2021-11-28 15:27:26
WEB打印大全
2023-06-30 14:35:15