Connecting to multiple databases with PDO.

This is a short tutorial on how to connect to multiple MySQL databases in the same script using the PDO object.

With the older mysql_ extension, you could connect to MySQL and change between databases like so:

As you can see, the same MySQL connection could beĀ used throughout the script. You’d simply switch the database by using the function mysql_select_db.

However, things work a bit differently with the PDO object:

With the PDO object, both the connection and the selected database are contained in the object. This is because the database is selected via the DSN string, which is passed in via the constructor!

Facebook Comments