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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示