使用PDO的多个数据库 [英] Multiple Databases using PDO

查看:81
本文介绍了使用PDO的多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用PDO很陌生,我想对其进行设置,这样我可以在需要时拥有多个数据库.因此,我创建了一个函数,该函数允许您传递数据库名称以用作何时何地.

I'm pretty new to using PDO and I would like to set it up so I can have multiple databases as and when I need them. So I've created a function that allows you to pass a database name to be used as and when.

它确实在一定程度上起作用,因为它选择了您传入的数据库,但是即使数据库被遗漏或不正确,它仍然允许您从数据库中选择表和行,而这些表和行似乎是基于以下内容随机选择的MySQL用户.

It does work to a certain extent, as in it selects the database you pass in but even if the database is omitted or incorrect it still allows you to select tables and rows from a database which seems to be selected at random based on the MySQL user.

我想这不是主要问题,但我想让它到达不会选择任何数据的地方,除非已通过我的函数传递了数据库.

This isn't a major issue I suppose but I would like to get it to where it won't select any data unless a database has been passed to through my function.

下面是我的代码,对于您对如何更好地解决此问题的想法,我将不胜感激.谢谢.

My code is below and I would appreciate your thoughts on how I may better approach this. Thanks.

index.php

require 'app/cream.php';

try {

    $db = new Cream_Model();
    $db = $db->selectDb( 'cream' );

    $data = $db->query('SELECT * FROM users');
    foreach( $data as $row ) {
        print_r( $row );
    }

} catch( PDOException $e ) {

    echo 'An error has occurrred: ' . $e->getMessage() . '<br />';

}

Model.php

class Model {

    public $connection;

    public function connect() {

        try {

            $connection = new PDO( DB_DSN . ':' . DB_HOST, DB_USERNAME, DB_PASSWORD, array( PDO::ATTR_PERSISTENT => true ) );
            $connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $connection->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC );

        } catch( PDOException $e ) {

            echo 'An error has occurred: ' . $e->getMessage() . '<br />';
            die();

        }

        return $connection;

    }

}

Cream_Model.php

class Cream_Model extends Model {

    public $conn;

    public function selectDb( $db ) {

        try {

            $conn = $this->connect();
            $conn->exec( "USE $db" );

        } catch( PDOException $e ) {

            echo 'An error has occurred: ' . $e->getMessage() . '<br />';

        }

        return $conn;

    }

}

推荐答案

对于PDO,您不应直接执行USE dbname.

For PDO, you should NOT exec USE dbname directly.

我认为您有多个PHP脚本实例,并且每个实例执行USE dbname时都没有PDO知道发生了什么,这会导致整个混乱.

I think what is happening that you have multiple instances of PHP script, and when each one executes USE dbname without PDO being aware of it happening, and this causes whole mess.

相反,您应该在PDO连接字符串中指定dbname,例如'mysql:host=localhost;dbname=testdb'.这意味着在创建Model类之后,您不能真正在数据库之间切换.您应该预先知道数据库名称,并在Model构造函数中使用它.

Instead, you should specify dbname in your PDO connection string like 'mysql:host=localhost;dbname=testdb'. That means you cannot really switch between databases after creating your Model class. You should know your database name upfront and use it in Model constructor.

PDO文档中了解更多信息.

这篇关于使用PDO的多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆