Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between multiple database in PDO

Tags:

php

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 need for creating a new object.

like image 974
hablema Avatar asked Mar 06 '12 17:03

hablema


People also ask

Can we connect two databases at a time?

Yes you can. You can connect 2 Access DB with separate session names. and perform this activity. @Pawar Aditi (Conflux Systems)​ You can do this using array variables also without using database command also.

Can we connect multiple database in PHP?

If you want to use multiple MySQL databases in your PHP project then you need to create a separate connection variable for your databases and use variables according to the database on which you are performing an action.

How do I link multiple databases to one application?

so, based on user login, the application should connect different database server. For Ex: if user "xxx" login with credential and belogs to "ABC" company and the database is "ABC", then ABC data need to display on the web page.

How can I connect one database to another in PHP?

php $servername = "localhost"; $username = "username"; $password = "password"; $db = "dbname"; // Create connection $conn = mysqli_connect($servername, $username, $password,$db); // Check connection if (!$ conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>


1 Answers

I know that I am a couple of months late but you should be able to switch between databases from within your query.

examples:

$sql = "SELECT * FROM dbname.tablename";

$sql = "SELECT * FROM anotherdbname.anothertablename"

So even if your original $pdo object was used 'blahblah' as the dbname, you should still be okay based on the select examples I provided.

like image 51
Laz Avatar answered Sep 30 '22 22:09

Laz