store multiple pdo connections in an array

so i have been trying to debug this issue myself for a few days now and i can't seem to figure out why i'm not getting the results I expect.

My code is rather complex and for a DB connection to be establish it spans across 3 Classes and one config file.

but basically my end usage ends up being

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

this establishes a connection to my database by the alias of test the query returns results so i'm good so far.

now my issue is when i try to make a new PDO object.

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

this returns nothing because there is not table called test2 in my test1 object.

but if I do this

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

now this returns the same results from the first PDO object.

I have traced and tracked down every line of code to make sure that the correct parameters are being passed to my database class and that each connection is properly established to the corresponding databases.

now my question is, can you have more than one datbase pdo connection? if so is there a special flag that needs to be set in the PDO options? are my connections being cached somewhere and causing this confusion?

this is my PDO declaration in each new class object stored in my array of connections

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

edit my code that uses the connection

step 1: a call to the parent class

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;
        }

step 2: called from the parent class

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

step 3: called from 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();
        }
    }

        [...]
}

Are you sure that the hashing you're doing is enough to "namespace" each connection in the $this->_sys_helper array?

I suspect the problem lies in the first stage.

    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/71968.html

上一篇: PDO声明和阅读结果。 基本

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