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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置