Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to 2 databases at the same time in PHP

I am trying to connect to 2 databases on the same instance of MySQL from 1 PHP script.

At the moment the only way I've figured out is to connect to both databases with a different user for each.

I am using this in a migration script where I am grabbing data from the original database and inserting it into the new one, so I am looping through large lists of results.

Connecting to 1 database and then trying to initiate a second connection with the same user just changes the current database to the new one.

Any other ideas?

like image 680
Darryl Hein Avatar asked Oct 24 '08 21:10

Darryl Hein


People also ask

Is it possible to connect to multiple databases simultaneously?

This can be done several times to connect to different databases, with the restriction that it will only allow one connection to the same database. If you try to use a database from multiple instances of the same application either on the same computer or on different computers you will receive an error message.

Can we use two database in one project?

Generally, if one project consumes multiple databases, it is because it must consume different, often legacy sources of information that originated outside of this particular project. This is most common in Enterprise environments.

Can we connect two database in single application?

If you can work with single database, working with multiple is no different. You will need a connection string for each database. There rest is, as they say it, history.


5 Answers

You'll need to pass a boolean true as the optional fourth argument to mysql_connect(). See PHP's mysql_connect() documentation for more info.

like image 120
Lucas Oman Avatar answered Oct 13 '22 22:10

Lucas Oman


If your database user has access to both databases and they are on the same server, you can use one connection and just specify the database you want to work with before the table name. Example:

SELECT column
FROM database.table

Depending on what you need to do, you might be able to do an INSERT INTO and save a bunch of processing time.

INSERT INTO database1.table (column)
SELECT database2.table.column
FROM database2.table
like image 36
Joe Lencioni Avatar answered Oct 13 '22 23:10

Joe Lencioni


Lucas is correct. I assume that both the databases are hosted on the same host.

Alternatively, you can create only 1 db connection and keep swapping the databases as required. Here is pseudo code.

$db_conn = connect_db(host, user, pwd);
mysql_select_db('existing_db', $db_conn);
 -- do selects and scrub data --
mysql_select_db('new_db', $db_conn);
-- insert the required data --
like image 37
Gaurav Avatar answered Oct 13 '22 23:10

Gaurav


I would suggest using two connection handlers

   $old = mysql_connect('old.database.com', 'user', 'pass);
   mysql_select_db('old_db', $old);


   $new = mysql_connect('new.database.com','user','pass);
   mysql_select_db('new_db', $new)

   // run select query on $old
   // run matching insert query on $new
like image 21
The.Anti.9 Avatar answered Oct 13 '22 22:10

The.Anti.9


If it's an option, use PDO: you can have as many database connections open as you like.

Plus, assuming your executing the same queries over and over, you can use prepared statements.

like image 44
Gary Richardson Avatar answered Oct 13 '22 21:10

Gary Richardson