dtp87205 2013-04-26 08:19
浏览 132
已采纳

PDO连接 - 最大连接数

I have the following class:

<?php
class Database {

    protected static $_instance;
    protected $_connection;
    protected $_dbhost=DB_HOST;
    protected $_dbname=DB_DBNAME;
    protected $_username = DB_USER;
    protected $_password = DB_PASS;
    protected $_dbType = "mysql";

    /**
    * Singleton pattern implementation makes "new" unavailable
    */
    protected function __construct()
    {
        $this->_connection = 
            new PDO($this->_dbType . ":host=" . $this->_dbhost . ";dbname=" . $this->_dbname, $this->_username, $this->_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_PERSISTENT => true));
    }

    public function getConnection()
    {
        return $this->_connection;
    }

    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
    * Singleton pattern implementation makes "clone" unavailable
    */
    protected function __clone()
    {}
}

?>  

From: https://stackoverflow.com/a/3010486

Now I have a second class, which has all the functions that access the DB.

My question:
I had a problem with max connections in my script, therefore I use the new Database class. In my helper class I do it like this:

<?php   
class helper {

    function getAllInvitesFromPlayer($uid) {
        $sql = "SELECT request_id FROM ".DBPREFIX."invites WHERE inviter_id = :uid AND joined = 1";

        try {
            $db = Database::getInstance()->getConnection();
            $stmt = $db->prepare($sql);
            $stmt->bindParam("uid", $uid);
            $stmt->execute();
            $content = $stmt->fetch(PDO::FETCH_LAZY);
            $db = null;
            return $content;
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }
}

After the "try" is it correct to use the $db = Database::getInstance()->getConnection(); or shall I "outsource" it to a class variable and access it like $this->_db; in each function and try ?
What is better to avoid getting too much connections to my db?

And its surely better just to initialize the helper class once in a file, right? and not always calling $helper = new helper() because this would always create a new DB connection, right?

Thank you for your help!

  • 写回答

1条回答 默认 最新

  • duanliao5995 2013-04-26 08:56
    关注

    It doesn't really matter.
    As long as you are using getInstance(), it would be always the same single connection, no matter which way or where you call it.

    For sake of incapsulation, it's better to assign db connection to a class variable.

    Also note that your use of try..catch is wrong. It shouldn't be there.

    So, something like this

    <?php   
    class helper {
    
        protected function __construct()
        {
            $this->db = Database::getInstance()->getConnection();
        }
    
        function getAllInvitesFromPlayer($uid) {
            $sql = "SELECT request_id FROM ".DBPREFIX."invites WHERE inviter_id = ? AND joined = 1";
            $stmt = $this->db->prepare($sql);
            $stmt->execute(array($uid));
            return $stmt->fetchColumn(); // will return one invite actually
            //or
            return $stmt->fetchAll(PDO::FETCH_COLUMN, 0); // will return ALL invites indeed
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘