Switch between multiple database in PDO

后端 未结 5 893
陌清茗
陌清茗 2020-12-10 01:48

I\'m new to PDO. I would like to know if there is anything similar to mysql_select_db in PDO, so that i can switch between different databases during runtime without the nee

5条回答
  •  清歌不尽
    2020-12-10 02:37

    It looks like PDO does not have database switching because not every database engine supports it.

    AFAIK PostgreSQL does not have database switching, but offer schemas and u can switch between those.

    However if you're using mysql check if this works for you:

    $pdo = new PDO('mysql:dbname=db1;host=127.0.0.1','user','pass');
    
    $sql = 'select count(*) from table_name';
    
    $res = $pdo->query($sql);
    print_r($res->fetchAll());
    
    $pdo->exec('USE db2');
    
    $res = $pdo->query($sql);
    print_r($res->fetchAll());
    

提交回复
热议问题