Tuesday, 15 January 2013

php - Pros and cons of connecting more than one database in single script -


Assume that the user has two databases hosted on one host and I need to join them so that I Any time you can add a connection code without using a table.

I implemented it in CodeIgniter to add the authentication details of both the databases in the database.php file and load the required database with the $-> Load-> Database ('dbname'); In the script

Now, for the basic PHP, we can do this:

  mysql_connect ('host', 'user', 'password', 'Port', 'dbname'); // Connection with a database  

This was connected to my first database.

Now, I want to connect to another database:

1) I have not closed up the connection and with the other

  mysql_connect (' Host ',' user ',' password ',' port ',' dbname1 ');  

2) Would it be bad practice to do this? Will it consume more items? Do we have to first stop anyway?

It does not need to open just 2 connections to use tables from 2 databases on the same server You need to use the proper notation of the database This means that you can join tables in different databases in a single question

  select t1.col1, t1 Col2, t2.col2, t2.col2 to db1.table1 as t 1 join db2 .table1 as t 2 on t1col1 = t2col3  

therefore If you have connected to DB1, then you can use the DB2 Tables and if you are connected to DB2, then you can use the DB1 table.


No comments:

Post a Comment