찾다
php教程php手册PHP PDO优化数据库操作类 多数据库驱动类

PHP PDO优化数据库操作类 多数据库驱动类

Jun 06, 2016 pm 07:37 PM
pdophp한 번최적화작동하다데이터 베이스정돈하다운전하다

就是做一下整理 PHP PDO类操作。简化操作流程 更多内容 http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/pdo.class.php 点击链接加入群【微信开发探讨群】:http://jq.qq.com/?_wv=1027gt;http://www.oschina.net/code/snippet_2276613_46605

就是做一下整理 PHP PDO类操作。简化操作流程
更多内容
http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/pdo.class.php
点击链接加入群【微信开发探讨群】:http://jq.qq.com/?_wv=1027&k=csNcd9
群号:330393916
欢迎浏览:www.zcstrong.com
QQ:2444756311

微信红包接口API - 拓展微信公众平台通用接口API(PHP版) --> http://www.oschina.net/code/snippet_2276613_46605
<?php
/**
 * Description of pdo
 *
 * @author Administrator
 */
class ZcPdo {
    public  $dbh        = "";   //全局连接Object;
    //public  $sth        = "";   //预处理参数
    private $dsn        = "";
    private $user       = "";
    private $password   = "";
    public  $returnAy   = array('errcode' => '','errmsg' => '');
    
    //构造函数 - 初始化连接
    public function __construct($dsn, $user, $password) {
        $this->dsn      = $dsn;
        $this->user     = $user;
        $this->password = $password;
        $this->pdoConnect();
    }
    
    private function pdoConnect(){
        try {
            $this->dbh = new PDO($this->dsn, $this->user, $this->password);
            return $this->dbh;
        }
        catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
            exit();
        }
    }
    
    //设置PDO参数
    public function zcAttribute($attribute, $value){
        $this->dbh->setAttribute($attribute, $value);
    }
    
    public function zcLastId($name = NULL){
        return $this->dbh->lastInsertId($name);
    }
    //数据库单语句执行操作
    public function zcExec($param) {
        try {
            $rows = $this->dbh->exec($param);//影响行数
            return $this->zcLog(TRUE, $rows);
        }
        catch (PDOException $e) {
            return $this->zcLog(FALSE, $e->getMessage());
        }        
    }
    
    //格式化数据
    public function zcQuote($string){
        return $this->dbh->quote($string);
    }
    
    //批量处理格式化数据
    public function zcBatchQuote($data){
        $result = "";
        if(!empty($data) && (is_array($data) || is_object($data))){
            foreach($data as $key => $value){
                if(!empty($value) && (is_array($value) || is_object($value))){
                    $result[$key] = $this->zcBatchQuote($value);
                }
                else{
                    $result[$key] = $this->zcQuote($value);
                }
            }
        }
        else {
            $result = $this->zcQuote($data);
        }
        return $result;
    }
    
    //数据库预处理操作 - 获取全部数据
    public function zcFetchAll($statement ,$parameter = NULL ,$type = PDO::FETCH_ASSOC){
        try{
            $sth = $this->dbh->prepare($statement);
            //$sth->execute($parameter);
            $sth->execute($this->zcBatchQuote($parameter));
            $result = $sth->fetchAll($type);
            if(!empty($result) && is_array($result)){
                return $this->zcLog(TRUE, $result);
            }
            else{
                return $this->zcLog(TRUE, NULL);
            }
        }
        catch (PDOException $e) {
            return $this->zcLog(FALSE, $e->getMessage());
        }
    }

    //数据库预处理操作 - 获取一行数据
    public function zcFetchRow($statement ,$parameter = NULL ,$type = PDO::FETCH_ASSOC){
        try{
            $sth = $this->dbh->prepare($statement);
            $sth->execute($this->zcBatchQuote($parameter));
            $result = $sth->fetch($type);
            if(!empty($result) && is_array($result)){
                return $this->zcLog(TRUE, $result);
            }
            else{
                return $this->zcLog(TRUE, NULL);
            }
        }
        catch (PDOException $e) {
            return $this->zcLog(FALSE, $e->getMessage());
        }
    }

    //数据库预处理操作 - 获取一个数据
    public function zcFetchOne($statement ,$parameter = NULL){
        try{
            $sth = $this->dbh->prepare($statement);
            $sth->execute($this->zcBatchQuote($parameter));
            $result = $sth->fetch(PDO::FETCH_NUM);
            if(!empty($result) && is_array($result)){
                return $this->zcLog(TRUE, $result[0]);
            }
            else{
                return $this->zcLog(TRUE, NULL);
            }
        }
        catch (PDOException $e) {
            return $this->zcLog(FALSE, $e->getMessage());
        }
    }
    
    //开始事务
    public function zcBegin(){
        $this->dbh->beginTransaction();
    }

    //提交事务
    public function zcCommit(){
        $this->dbh->commit();
    }
    
    //回滚事务
    public function zcRollBack(){
        $this->dbh->rollBack();
    }

    //预处理事务执行语句
    public function zcPtmTstQuery($statement ,$parameter = NULL){
        try{
            $this->zcBegin();
            $result = $this->dbh->prepare($statement)->execute($parameter);
            $this->zcCommit();
            return $this->zcLog(TRUE, $result);
        }
        catch (PDOException $e) {
            $this->zcRollBack();
            return $this->zcLog(FALSE, $e->getMessage());
        }
    }

    //预处理执行语句
    public function zcPtmQuery($statement ,$parameter = NULL){
        try{
            $result = $this->dbh->prepare($statement)->execute($parameter);
            return $this->zcLog(TRUE, $result);
        }
        catch (PDOException $e) {
            return $this->zcLog(FALSE, $e->getMessage());
        }
    }
    
    //Query执行
    public function zcQuery($statement,$type = PDO::FETCH_ASSOC){
        try{
            $result = $this->dbh->query($statement,$type);
            return $this->zcLog(TRUE, $result);
        }
        catch (PDOException $e) {
            return $this->zcLog(FALSE, $e->getMessage());
        }
    }
    
    //日志LOG
    public function zcLog($errcode , $errmsg){
        $this->returnAy = array();
        $this->returnAy['errcode'] = $errcode;
        $this->returnAy['errmsg'] = $errmsg;
        $this->returnAy['errtime'] = date("Y-m-d H:i:s",time());
        return $this->returnAy;
    }
}
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SecList

SecList

SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

ZendStudio 13.5.1 맥

ZendStudio 13.5.1 맥

강력한 PHP 통합 개발 환경