thinkphp 连接pgsql 类执行增删改查 【源码】前提安装 pdo 与pdo_pgsql依赖

本文介绍如何使用ThinkPHP框架连接并操作PostgreSQL数据库,包括安装PDO和pdo_pgsql扩展,以及执行增删改查的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
/**
 * @deprecated thinkphp  pgSql 数据库链接 执行增删改查 需要安装pdo 与pdo_pgsql 扩展
 * @datetime 2022-01-25 12:00
 * Class PGSqlService
 * @author 飞,只是一种状态
 */

class PGSqlService
{
    protected static $_dbh = null; //静态属性,所有数据库实例共用,避免重复连接数据库
    protected $_dbType = 'pgsql';
    protected $_pconnect = true;   //是否使用长连接
    protected $_host = 'localhost';
    protected $_port = 3306;
    protected $_user = 'root';
    protected $_pass = 'root';
    protected $_dbName = 'pg_db'; //数据库名
    protected $_sql = false;   //最后一条sql语句
    protected $_where = '';
    protected $_order = '';
    protected $_limit = '';
    protected $_field = '*';
    protected $_clear = 0;      //状态,0表示查询条件干净,1表示查询条件污染
    protected $_trans = 0;      //事务指令数
    protected $_dev   = '';

    /**
     * @deprecated  初始化类
     * @param array $conf 数据库配置
     * PGSqlService constructor.
     */
    public function __construct($conf = array()) {
        ini_set('memory_limit', '-1');
        class_exists('PDO') or die('PDO: 此类不存在');
        if(!empty($conf))
        {
            $this->_dbType = $conf['type'];
            $this->_host   = $conf['hostname'];
            $this->_port   = $conf['hostport'];
            $this->_user   = $conf['username'];
            $this->_pass   = $conf['password'];
            $this->_dbName = $conf['database'];
            $this->_dev    = $conf['pg_dev'];
        }
        if ( is_null(self::$_dbh) ) {  $this->_connect();  }
    }


    /**
     * @deprecated 连接数据库的方法 整合
     */
    protected function _connect() {
        $dsn = $this->_dbType.':host='.$this->_host.';port='.$this->_port.';dbname='.$this->_dbName;

        $options = $this->_pconnect ? array(\PDO::ATTR_PERSISTENT=>true) : array();
        try {
            $dbh = new \PDO($dsn, $this->_user, $this->_pass, $options);
            $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);  //设置如果sql语句执行错误则抛出异常,事务会自动回滚
            $dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);           //禁用prepared statements的仿真效果(防SQL注入)
        } catch (\PDOException $e) {
            die('Connection failed: ' . $e->getMessage());
        }
        $dbh->exec("SET CLIENT_ENCODING='UTF-8';");
        self::$_dbh = $dbh;
    }

    /**
     * @deprecated 字段和表名添加 `符号
     * 保证指令中使用关键字不出错 针对mysql
     * @param string $value
     * @return string
     */
    protected function _addChar($value) {
        if ('*'==$value || 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值