vijdev Posted May 11, 2010 Share Posted May 11, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/201321-2-databases-simultaneously-update-in-one-script/ Share on other sites More sharing options...
DavidAM Posted May 11, 2010 Share Posted May 11, 2010 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 ...'); Quote Link to comment https://forums.phpfreaks.com/topic/201321-2-databases-simultaneously-update-in-one-script/#findComment-1056216 Share on other sites More sharing options...
vijdev Posted May 11, 2010 Author Share Posted May 11, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/201321-2-databases-simultaneously-update-in-one-script/#findComment-1056221 Share on other sites More sharing options...
Sudantha Posted May 11, 2010 Share Posted May 11, 2010 i think you can fire trigger for this ! Quote Link to comment https://forums.phpfreaks.com/topic/201321-2-databases-simultaneously-update-in-one-script/#findComment-1056253 Share on other sites More sharing options...
vijdev Posted May 11, 2010 Author Share Posted May 11, 2010 whats fire trigger? Quote Link to comment https://forums.phpfreaks.com/topic/201321-2-databases-simultaneously-update-in-one-script/#findComment-1056280 Share on other sites More sharing options...
Muddy_Funster Posted May 11, 2010 Share Posted May 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/201321-2-databases-simultaneously-update-in-one-script/#findComment-1056293 Share on other sites More sharing options...
DavidAM Posted May 11, 2010 Share Posted May 11, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/201321-2-databases-simultaneously-update-in-one-script/#findComment-1056433 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.