在数组中存储多个pdo连接

时间:2012-09-20 05:38:28

标签: php pdo

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

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

但基本上我的最终用法最终是

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

这通过别名test与我的数据库建立连接,查询返回结果,所以到目前为止我还不错。

现在我的问题是当我尝试制作一个新的PDO对象时。

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

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

但如果我这样做

$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();
        }
    }

        [...]
}

1 个答案:

答案 0 :(得分:1)

您确定您正在进行的散列足以“命名”$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;
    }