Jump to content

2 databases simultaneously update in one script?


vijdev

Recommended Posts

i want to execute 2 databases simultaneously update in one script?

certain inputs go to one DB, and certain to other DB.

So I believe both DB need to be active?..how to achive this using mysql_select_db()?

 

also please tell me how to do this:

1.the connection parameters are same

2.the connection parameters are different

Link to comment
Share on other sites

you will have to have two different connections

 

$cn1=mysql_connect(server, user, password, true);
$cn2 = mysql_connect(server, user, password, true);
mysql_select_db(database1, $cn1);
mysql_select_db(database2, $cn2);

mysql_query('UPDATE ...', $cn1);
mysql_query('UPDATE ...', $cn2);

 

If the databases are on the same server, you can use a single connection and qualify the table names with the database name:

mysql_query('UPDATE database1.tablename ...');
mysql_query('UPDATE database2.tablename ...');

Link to comment
Share on other sites

for using below:

---------------------

mysql_query('UPDATE database1.tablename ...');

mysql_query('UPDATE database2.tablename ...');

---------------------

 

should this be preceded by a mysql_select_db?

if yes, how?

if no, why? - is it not mandatory to use mysql_select_db after establishing a connection?

Link to comment
Share on other sites

No you don't need to assign a different mysql_select_db() to each query.  As David said, as long as its on the same server you can point your query to the database in the query it's self.  Either by using  DATABASE_NAME.table_name or by including a USE statement at the start of your sql.

 

As for Triggers... see HERE

But as you don't know what they are, I'm thinking it's probably best you don't try to use them at this stage.

 

 

Link to comment
Share on other sites

Unless you are using mysqli and multi_query, you cannot put a use statment in with the query to be executed

 

i.e. this will not work

mysql_query('USE database1; UPDATE table ...');

 

You could select the database just before executing each query

mysql_select_db('database1');
mysql_query('UPDATE table ...');
mysql_selectdb('database2');
mysql_query('UPDATE table ...');

 

but it is not strictly necessary.  As long as every table reference is qualified by a database name, you don't have to specify a default database.  Also, if you do specify a default database, the is nothing preventing you from access data in another database (on the same server).

 

mysql_select_db(database1);
mysql_query(SELECT a.Name, b.Name FROM Users a JOIN database2.Users b ON a.ID = b.ID

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.