Trizen Posted November 19, 2009 Share Posted November 19, 2009 how do i connect to two databases and submit a form to them at the same time? Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/ Share on other sites More sharing options...
abazoskib Posted November 19, 2009 Share Posted November 19, 2009 assign your mysql_connect() function to a variable, i.e. $link1, $link2. Then when calling a query use mysql_query($query,$link1) or mysql_query($query,$link2) respectively. Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960825 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Author Share Posted November 19, 2009 i dont want them to vary which it sends to i want it to always submit to both at the same time. Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960827 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Author Share Posted November 19, 2009 ok im kinda new at the database thing, i have always used forms that emailed me... i know im a noob, but now i need a form to post to two seperate databases. 1 for me and one for another office. thanks Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960835 Share on other sites More sharing options...
rajivgonsalves Posted November 19, 2009 Share Posted November 19, 2009 as abazoskib said you'll have to do it that way there is no other way. you have to make two different links and run queries on them Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960836 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Author Share Posted November 19, 2009 so something like <?php $handle_db1 = mysql_connect("localhost","myuser","apasswd"); $handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd"); mysql_select_db("db1",$handle_db1); mysql_select_db("db2",$handle_db2); $query = "select * from test"; $which = $handle_db1; mysql_query($query,$which); $query = "select * from test"; $which = $handle_db2; mysql_query($query,$which); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960851 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Author Share Posted November 19, 2009 or like $link=mysql_connect("localhost", "name", "password", true); $link1=mysql_connect("localhost", "name", "password", true); $mysql_query($qry,$link); $mysql_query($qry1,$link1); Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960852 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Author Share Posted November 19, 2009 is there anyway to do something like this $link=mysql_connect("localhost", "name", "password", true); $link1=mysql_close($link); $link2=mysql_connect("localhost", "name", "password", true); $mysql_query($qry,$link,$link1,$link2); Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960854 Share on other sites More sharing options...
Zane Posted November 19, 2009 Share Posted November 19, 2009 You can always specify the database explicitly in the query. But you still have to select a database to begin with no matter what. i.e. you have to use mysql_select_db AT LEAST once For example mysql_connect('localhost','user','pass'); mysql_select_db("dataOne"); $query = " INSERT into `sometable` (name) VALUES ('A name'); //Inserts into dataOne INSERT into `dataTwo`.`sometable` (name) VALUES ('A name'); //Inserts into dataTwo "; $r = mysql_query($query); This is assuming you have two databases (dataOne and dataTwo).. Each with a table called someTable.. that has a field called name. Edit: Also, assuming your database(s) or table(s) isn't named a reserved word. you can do this $query = " INSERT into sometable (name) VALUES ('A name'); //Inserts into dataOne INSERT into dataTwo.sometable (name) VALUES ('A name'); //Inserts into dataTwo "; Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960855 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Author Share Posted November 19, 2009 How bout this <?php $dataOne = mysql_connect('localhost','user','pass'); $dataTwo = mysql_connect('localhost','user','pass'); mysql_connect('localhost','user','pass'); mysql_select_db("dataOne"); $query = "INSERT into `sometable` (name) VALUES ('A name'); //Inserts into dataOne"; $r = mysql_query($query); mysql_close($dataOne); mysql_select_db("dataTwo"); $query = "INSERT into `sometable` (name) VALUES ('A name'); //Inserts into dataTwo"; $r = mysql_query($query); mysql_close($dataTwo); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960857 Share on other sites More sharing options...
rajivgonsalves Posted November 19, 2009 Share Posted November 19, 2009 this should be it <?php mysql_connect('localhost','user','pass'); mysql_select_db("dataOne"); $query = "INSERT into `sometable` (name) VALUES ('A name'); //Inserts into dataOne"; $r = mysql_query($query); mysql_select_db("dataTwo"); $query = "INSERT into `sometable` (name) VALUES ('A name'); //Inserts into dataTwo"; $r = mysql_query($query); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960861 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Author Share Posted November 19, 2009 dont i need to define dataOne and dataTwo somewhere Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960866 Share on other sites More sharing options...
Zane Posted November 19, 2009 Share Posted November 19, 2009 That would work.. but it's outright repetitive. no offense I'll lay it out for you. $dataOne = mysql_connect('localhost','user','pass'); $dataTwo = mysql_connect('localhost','user','pass'); //Same value as above mysql_connect('localhost','user','pass'); //Same as above too There's no point in assigning mysql_connect to a variable UNLESS you have MULTIPLE SERVERS to connect to. Say you had the login credentials for a database at yahoo.com AND you had your own personal server at localhost. THEN you would have VARIABLES. Otherwise, mysql_connect is going to connect regardless to whatever you tell it to. You said in your op that you only had to databases.. not two servers. So the question is. Are these databases hosted on the same server or not? If they are, then my solution should work just fine. If they're not, then yes, THAT (what you supplied) is exactly how you would go about posting to multiple databases on separate servers. Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960868 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Author Share Posted November 19, 2009 ok thanks im not sure yet as the second database has not been setup and im not sure if its going to be on the same server or the backup server. so i will take that into account. Thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/182121-mysql_insert-to-two-databases/#findComment-960873 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.