php操作mysql pdo自定义类

php版本 >= 5.6


class pdomysql
{
    public function __construct() 
    {
    //数据库设置
        $this->dbconfig =   [
                                'DB_TYPE' => 'mysql', // 数据库类型
                                'DB_HOST' => '127.0.0.1', // 服务器地址
                                'DB_NAME' => 'test', // 数据库名
                                'DB_USER' => 'root', // 用户名
                                'DB_PWD' => '123456', // 密码
                                'DB_PORT' => '3306', // 端口
                                'DB_PREFIX' => '', // 数据库表前缀
                                'DB_CHARSET' =>  'utf8mb4',
                                'DB_DEBUG' => false,
        ];
        $this->pdocon = "";//数据连接session
    }
    
    //执行mysql查询
    public function pdo_fetchall( $sql )
    {
        $pdo = $this->pdocon ? $this->pdocon : $this -> connect();
        $rs = $pdo -> query( $sql );
        $tmp = [];
        $counter = 0;
        while( $arr = $rs -> fetch( PDO::FETCH_ASSOC ) )
        {
            $tmp[] = $arr;
            $counter ++;
        }
        
        if( $this->pdocon=="" )
            $this -> close( $pdo );
        
        if( $counter )
            return $tmp;
        else
            return false;
    }

    //执行mysql语句,返回一条数组
    public function pdo_fetch( $sql )
    {
        $pdo = $this->pdocon ? $this->pdocon : $this -> connect();
        $rs = $pdo -> query( $sql );
        $arr = $rs -> fetch( PDO::FETCH_ASSOC );
        
        if( $this->pdocon=="" )
            $this -> close( $pdo );
        
        if( $arr )
            return $arr;
        else
            return false;
    }

    //执行mysql语句
    public function execute( $sql )
    {
        $pdo = $this->pdocon ? $this->pdocon : $this -> connect();
        $rs = $pdo -> exec( $sql );
        if( $rs===false )
            return false;
        
        if( preg_match( "/insert into/i", $sql ) )
        {
            $lastid = $pdo -> lastInsertId();
            $return = $lastid;
        }
        else
        {
            $return = $rs;
        }

        if( $this->pdocon=="" )
            $this -> close( $pdo );
        
        return $return;
    }

    //写入数据表
    public function pdo_insert( $tablename, $data )
    {
        $sql = "insert into `{$tablename}` ";

        $key = $value = [];
        foreach( $data as $k => $v )
        {
            $key[] = $k;
            $value[] = "'{$v}'";
        }
        $key = implode( "," , $key);
        $value = implode( "," , $value);

        $sql = "insert into `{$tablename}` ({$key}) values({$value});";
        $pdo = $this->pdocon ? $this->pdocon : $this -> connect();
        $rs = $pdo -> exec( $sql );
        $lastid = $pdo -> lastInsertId();
        
        if( $this->pdocon=="" )
            $this -> close( $pdo );
        
        return $lastid ? $lastid : 0;
    }

    //更新数据表
    public function pdo_update( $tablename, $data, $condition )
    {
        $sql = "update `{$tablename}` set ";
        
        $value = [];
        foreach( $data as $k => $v )
        {
            if( strpos( $k, '+=') )
            {
                $k = explode( ' ' , $k )[0];
                $value[] = "{$k}={$k}+{$v}";
            }
            elseif( strpos( $k, '-=') )
            {
                $k = explode( ' ' , $k )[0];
                $value[] = "{$k}={$k}-{$v}";
            }
            else
                $value[] = "{$k}='{$v}'";
        }
        $value = implode( ",", $value );
        $sql .= $value;

        $sql .= " where ";

        $where = [];
        foreach( $condition as $k => $v )
        {
           $where[] = "{$k}='{$v}'"; 
        }
        $where = implode( " AND " , $where );
        $sql .= $where;

        $pdo = $this->pdocon ? $this->pdocon : $this -> connect();
        $rs = $pdo -> exec( $sql );
        if( $rs===false )
            return false;
        
        if( $this->pdocon=="" )
            $this -> close( $pdo );
        
        return $rs;
    }
    
    public function connect()
    {
        $dsn = "mysql:host={$this->dbconfig['DB_HOST']};port={$this->dbconfig['DB_PORT']};dbname={$this->dbconfig['DB_NAME']};charset={$this->dbconfig['DB_CHARSET']}";
        $pdo = new PDO( $dsn, $this->dbconfig['DB_USER'], $this->dbconfig['DB_PWD'] );
        
        return $pdo;
    }
    
    public function close( $pdo )
    {
        $pdo = null;
    }

    public function pdo_begin()
    {
        $this->pdocon = $this -> connect();
        $this->pdocon -> beginTransaction();
    }

    public function pdo_rollback()
    {
        $this->pdocon -> rollBack();
        $this -> close( $this->pdocon );
        $this->pdocon = "";
    }

    public function pdo_commit()
    {
        $this->pdocon -> commit();
        $this -> close( $this->pdocon );
        $this->pdocon = "";
    }
}

点赞(0)
发表
评论
返回
顶部