使用PDO和MySQL的“训练模式”

I'm planning to do a "training mode" to my system. When it's activated, the systems database connection would get table users from the database 1 but tables customers and products from the database 2.

What would be the best way to do this? I have following database connection (using PDO).

$pdo = new PDO(DB_TYPE.':host='.DB_HOST.'; dbname='.DB_NAME, DB_USER, DB_PASS);

The system looks for training mode status from users and if it's true, it should connect another database's customers and products. Can I get some of the tables from one database and some of the other?

Yes of course you can, all you need to do is create another PDO connection to the other database.

// original connection
$pdo = new PDO(DB_TYPE.':host='.DB_HOST.'; dbname='.DB_NAME, 
               DB_USER, DB_PASS);

// test mode connection
$pdo_test = new PDO(DB_TYPE.':host='.DB_HOST.'; dbname='.DB_NAME_TEST, 
               DB_USER_TEST, DB_PASS_TEST);

Then its just a f.l.o.c to make sure you use the correct connection object in whichever query you are execution.

One way would be to simply use absolute table references, e.g. dbname.tablename.fieldname:

$source = 'training';
$sql = "SELECT foo FROM $source.table";

Just set $source somewhere central and use it in all of your queries.

Alternatively, you COULD set up two completely separate dbs with identical table structures, and just connect to the appropriate one. This would mean connecting TWICE. once to get user data and figure out where to connect, and then actually make that connection.

You wouldn't have to rewrite all your queries into the absolute form, but now you've got to maintain two totally separate dbs and datasets.