将多个pdo连接存储在一个数组中

所以我一直试图自己调试这个问题几天,现在我似乎无法弄清楚为什么我没有得到我期望的结果。

我的代码相当复杂,为了建立数据库连接,它跨越3个类和一个配置文件。

但基本上我的最终用法结束了

$this->db('test')->query('SELECT * FROM test1');

这建立了一个连接到我的数据库的别名test查询返回结果,所以我很好迄今。

现在我的问题是当我尝试创建一个新的PDO对象。

$this->db('test2')->query('SELECT * FROM test2');

这不会返回任何内容,因为我的test1对象中没有名为test2表。

但如果我这样做

$this->db('test2')->query('SELECT * FROM test1');

现在这将返回与第一个PDO对象相同的结果。

我已经跟踪并追踪了每行代码,以确保将正确的参数传递给我的数据库类,并且将每个连接正确建立到相应的数据库。

现在我的问题是,你可以有多个数据库pdo连接吗? 如果有的话是否需要在PDO选项中设置特殊标志? 我的连接被缓存在什么地方并导致这种混淆?

这是存储在我的连接数组中的每个新类对象中的我的PDO声明

try
        {
            $this->_con = new PDO(
                "mysql:host=" . $host . ";
                 port=" . $port . ";
                 dbname=" . $name, $user, $pass
            );

            $this->_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        } catch(PDOException $e) {

            // TODO: push all $e methods to the developer debugger
            echo "Database Error: ". $e->getMessage();
        }

编辑使用连接的代码

第1步:调用父类

public function __call($name, $params)
        {
            $class = $name . '_system_helper';
            $hash  = md5($class . $params);

            if (class_exists($class))
            {
                if (!array_key_exists($hash, $this->_sys_helper))
                {
                    if (method_exists($class, 'init'))
                    {
                        $this->_sys_helper[$hash] = call_user_func_array(array($class, 'init'), $params);

                    } else {

                        $this->_sys_helper[$hash] = call_user_func_array($class, $params);
                    }
                }

                return $this->_sys_helper[$hash];
            }

            return null;
        }

第2步:从父类中调用

class DB_System_Helper extends Jinxup
    {
        private $_con = null;

        public function __construct($end = null)
        {
            $mode = null;
            $host = null;
            $name = null;
            $user = null;
            $pass = null;
            $port = null;

            if (isset($this->config['database']['mode']))
            {
                $mode = $this->config['database']['mode'] == 'production' ? 'production' : 'development';

                if (count($this->config['database'][$mode]) > 1)
                {
                    foreach ($this->config['database'][$mode] as $key => $database)
                    {
                        if ($database['@attr']['alias'] == $end)
                        {
                            $host = $this->config['database'][$mode][$key]['host'];
                            $name = $this->config['database'][$mode][$key]['name'];
                            $user = $this->config['database'][$mode][$key]['user'];
                            $pass = $this->config['database'][$mode][$key]['pass'];
                            $port = $this->config['database'][$mode][$key]['port'];
                        }
                    }

                } else {

                    $host = $this->config['database'][$mode]['host'];
                    $name = $this->config['database'][$mode]['name'];
                    $user = $this->config['database'][$mode]['user'];
                    $pass = $this->config['database'][$mode]['pass'];
                    $port = $this->config['database'][$mode]['port'];
                }

                $this->_con = new PDO_Database_Helper($host, $name, $user, $pass, $port);

            } else {

                echo 'No database mode specified';
            }
        }

        public function __call($name, $param)
        {
            return call_user_func_array(array($this->_con, $name), $param);
        }
    }

第3步:从DB_System_Helper调用

class PDO_Database_Helper extends Jinxup
{
    private $_con = null;
    private $_id  = 0;

    public function __construct($host, $name, $user, $pass, $port = 3306)
    {
        try
        {
            $this->_con = new PDO(
                "mysql:host=" . $host . ";
                 port=" . $port . ";
                 dbname=" . $name, $user, $pass
            );

            $this->_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        } catch(PDOException $e) {

            // TODO: push all $e methods to the developer debugger
            echo "Database Error: ". $e->getMessage();
        }
    }

        [...]
}

你确定你正在做的哈希足以“命名空间” $this->_sys_helper数组中的每个连接吗?

我怀疑问题在于第一阶段。

    public function __call($name, $params)
    {
        $class = $name . '_system_helper';
        $hash  = md5($class . $params);

        if (class_exists($class))
        {
            if (!array_key_exists($hash, $this->_sys_helper))
            {
                if (method_exists($class, 'init'))
                {
                    $this->_sys_helper[$hash] = call_user_func_array(array($class, 'init'), $params);

                } else {

                    $this->_sys_helper[$hash] = call_user_func_array($class, $params);
                }
            }

  >>>>>>>>>>>>>> are you sure this is not returning the wrong
  >>>>>>>>>>>>>> connection because of how the hashing is working?
            return $this->_sys_helper[$hash];
        }

        return null;
    }
链接地址: http://www.djcxy.com/p/71967.html

上一篇: store multiple pdo connections in an array

下一篇: How is a PDO result set stored