Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在阵列中存储多个pdo连接_Php_Pdo - Fatal编程技术网

Php 在阵列中存储多个pdo连接

Php 在阵列中存储多个pdo连接,php,pdo,Php,Pdo,所以我已经试着自己调试这个问题好几天了,我似乎不明白为什么我没有得到预期的结果 我的代码相当复杂,要建立数据库连接,它跨越3个类和一个配置文件 但基本上我的最终用途是 $this->db('test')->查询('SELECT*fromtest1') 这将通过别名test与我的数据库建立连接。查询将返回结果,因此到目前为止我还不错 现在我的问题是当我尝试创建一个新的PDO对象时 $this->db('test2')->查询('SELECT*fromtest2') 这不会返回任何内容,因为在我的t

所以我已经试着自己调试这个问题好几天了,我似乎不明白为什么我没有得到预期的结果

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

但基本上我的最终用途是

$this->db('test')->查询('SELECT*fromtest1')

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

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

$this->db('test2')->查询('SELECT*fromtest2')

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

但如果我这样做

$this->db('test2')->查询('SELECT*fromtest1')

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

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

现在我的问题是,您可以有多个datbase 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;
        }
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);
        }
    }
步骤2:从父类调用

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;
        }
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_系统_助手调用

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->\u sys\u 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;
    }

在我维护的一个框架中,我允许并利用使用PDO的多个数据库连接——这完全是可能的,并且非常方便地记录错误(当主连接发生回滚时,错误日志插入不会意外回滚)。更多地了解一下您的
$this->db('name')
方法会很有帮助。我真的想知道您是否因为对象引用而被绊倒了。这是一种有趣的方法——相当新颖。如何使用private$\u con member变量?这并不是说它对您有多大好处:我在配置文件中使用别名创建数据库连接对象-当我要创建新的查询对象时,每个连接都会成为一个包含在关联数组(key是连接别名)中的自包含对象,我将连接名称传递给它的构造函数-查询对象从名为DBM()的全局单例样式函数中检索连接对象,并在需要与数据库通信时使用它。因此,在配置中:
DBM()->useSetup(数组('main'=>newdbsetup('user','pass','db name');
Then,
query::factory('main'))->select()->from('login')->exec()->fetchAll();
我不明白这是怎么回事。u call和call_user_func_数组应该区分大小写。在顶部的示例中调用$this->db('dbname')不应该起作用,因为这样会查找“db_System_Helper”而不是“db_System_Helper”。也许可以尝试$this->db('dbname'))?我没有太多的帮助文件,因此某个地方可能会发生冲突。但我认为你是对的,我可能会在某个地方被对象引用绊倒。我认为更可能的是,你正在尝试创建两个不同的帮助文件,具有不同的设置数据-对象类型是相同的-但你需要确保它来自其他地方你的数组正确。是哈希匹配,我确实查看了一遍,并确保返回了正确的对象。这是我查看的第一件事。问题是我的代码创建时只有一个db连接,但由于发现我需要更多连接,所以我调整了代码以允许更多对象以别名存储。你确定吗数据库设置是否正确?
if($database['@attr']['alias']=$end)
您没有重复的别名值,是吗?没有,但我只是将别名传递给了一个公共变量,并在每次调用db对象时进行了vardump,似乎我的别名被某种方式交叉了。感谢您的想法!感谢您的框架链接,我将在修复此问题后提交并推送我的最新副本到github and发送给您学习的链接。