Jump to content

mysql_insert to two databases


Trizen

Recommended Posts

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); 

?> 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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
";

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

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.