Forum Moderators: coopster

Message Too Old, No Replies

2 mysql session on different db

2 mysql session on different db

         

paonza

5:34 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Hi all,
I need to copy records from a db to another...
Is it possible open 2 different mysql connection on different db and work with query on db1 and insert into db2?
is it possible?
could someone show me a pice of code?

regards,
Paonza

Salsa

6:14 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Paonza, Welcome to WebmasterWorld!

As to your question, sure it's possible, but it's not necessary. You can do it with one connection, just name the databases in your queries, like:

SELECT stuff FROM database1.table... 
INSERT INTO database2.table...

...this assumes that the connection's user has the necessary privileges for both databases and their affected tables.

I hope this helps.

paonza

6:46 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Thank you for your answer...
but... I do not need to connect the db?

i can referer to db1 directly inside mysql_query without open a connection?

ciao
Paonza

Salsa

7:10 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Yes, you do need to open a connection with mysql_connect(), but you don't need to select a database using mysql_select_db() if you name your databases in your queries. And you can query any number of databases that you wish--through the one connection--provided the user you named in mysql_connect() has the necessary privileges, and the databases are all on the same server.

Does that make sense?

paonza

7:40 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



uhmmmm....
the problem is I have 2 different db on 2 different server. So i need 2 different connection to 2 different IP address...

I need to sincornize a db with the remote one.

What do you think?

ciao
Paonza

Salsa

8:31 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Okay, well, then you do need two connections. I'm no expert on that, but you might try something like:

$db_link_1 = mysql_connect('hostname1','user','passwd');
$db_link_2 = mysql_connect('hostname2','user','passwd');

$sql1 = "SELECT stuff FROM database1.table... ";
$sql2 = "INSERT INTO database2.table... ";

mysql_query($sql1,$db_link_1);
mysql_query($sql2,$db_link_2);

When making your connections, the local server can be 'localhost', but for the remote connection you'll need to use that hostname.

I hope this helps.

paonza

12:44 am on Jan 5, 2005 (gmt 0)

10+ Year Member



thank you Salsa, thank you very much!

I'll try this as soon as possible and I'll post my results!

ciao
Paonza