3

I have to insert data in two different database's table. I have created database1 and table1 for database1, also i have created database2 and table2 for database2.

For inserting data i have written code,

$connect = mysql_connect("localhost","root",""); //database connection

mysql_select_db("database1",$connect); // select database1 
mysql_select_db("database2",$connect); // select database2 

$sql = mysql_query("INSERT INTO database1.table1 (contact_first, contact_last, contact_email) VALUES('abc','xyz','[email protected]')"); //insert record to first table
$sql1 =mysql_query("INSERT INTO database2.table2 (contact_first, contact_last, contact_email) VALUES('abc','xyz','[email protected]')"); //insert record to second table

please suggest me corrections for above code to insert data.

1

5 Answers 5

2

Try the following code:

$connect1 = mysql_connect("localhost","root","");
mysql_select_db("database1", $connect1);
$res1 = mysql_query("query",$connect1);

$connect2 = mysql_connect("localhost","root","",true);
mysql_select_db("database2", $connect2);
$res2 = mysql_query("query",$connect2);  

Note: So mysql_connect has another optional boolean parameter which indicates whether a link will be created or not. as we connect to the $connect2 with this optional parameter set to 'true', so both link will remain live.

Sign up to request clarification or add additional context in comments.

Comments

1

Simply connect to 1 database, insert new row, disconnect, connect to the other database, insert row into that one and disconnect.

Or you can use $connect1 and $connect2 to refer to each of them separately and do the insertion parallely.

EDIT: Btw you can select the database with the 4'th parameter of mysql_connect, no need to use mysql_select_db

And very important, you should write mysqli not mysql. Because mysql functions are not going to be supported for much longer.

2 Comments

But he has 1 connection, with 2 different db selections. Actually it should work like this.
@zoran404 it would be more useful if you add as an example by taking his code , by the way good technique
0

first create two database connections

$connect1 = mysql_connect("localhost","root",""); 
$connect2 = mysql_connect("localhost","root",""); 

Then select the database for each connection.

mysql_select_db("database1",$connect1); // select database1 
mysql_select_db("database2",$connect2); // select database2 

Then pass in a second argument for mysql_query which is the respective connection for the query.

mysql_query("SELECT ... ", $connect1);
mysql_query("SELECT ... ", $connect2);

1 Comment

Thanks for you answer it working after adding $connect2 = mysql_connect("localhost","root","",true);
0

Well, if there's a pattern in db names, tables and queries are exactly the same, you can use a loop:

for ($i = 1; $i <=2; $i++) {
    mysql_select_db("database".$i, $connect); 
    $sql = mysql_query("INSERT INTO table".$i." (contact_first, contact_last, contact_email) VALUES('abc','xyz','[email protected]')");
    mysql_close;
}

However, using mysql_* is strongly NOT recommended, as it is deprecated from the last stable PHP release and is considered unsafe. Use PDO or MySQLi instead. PHP's official site suggests the article "Choosing an API": http://www.php.net/manual/en/mysqlinfo.api.choosing.php

4 Comments

Why there is a loop for only two databases just to connect/insert and close this is not a generic solution
If the architecture increases, only the loop will be changed. What do you suggest as a solution, manually build each query? I would, but if the queries are exactly the same, I would use a loop
But think for a while no one will have the databases name like ...1,..2,..3 and so on
Why not :) We have a game which has local versions in 5 different countries. Every local version has its own database. i.e. game_en, game_fr, game_de, etc. which contains players, profiles etc same tables. If I want to update player's credits in each version, I would iterate through the language tags, append to game_ and run the query
0

Well, that's how i do it...

1 - connect --> that you are doing right 2 - check for errors 3 - USE a database (1) you want to put data in (and not 'SELECT') 4 - check for errors 5 - now INSERT items into the database THAT IS BEING USED - that is (1) 6 - check for errors 7 - USE the other database (2) 8 - check for errors 9 - INSERT the data into (2) - because that is the one in use now 10 - check for errors

Yes, be paranoid :P Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.