PHP封装的PDO数据库操作类实例

前端之家收集整理的这篇文章主要介绍了PHP封装的PDO数据库操作类实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP封装的PDO数据库操作类。分享给大家供大家参考,具体如下:

PHP;"> 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,-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,$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,-1); $str .= '),'; } $filed_str = implode(',',$fileds); $val_str = substr($str,-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,$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,$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,-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,$ch_url); curl_setopt($ch,CURLOPT_POST,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常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

猜你在找的PHP相关文章