douruhu4282 2015-09-14 18:16
浏览 21
已采纳

使用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?

  • 写回答

2条回答 默认 最新

  • dongwen9947 2015-09-14 18:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?