0
mysql_connect("localhost","root","");
mysql_select_db("database1");
mysql_query("INSERT INTO database2.categories (category_name,description,group_id,created)
  SELECT table1.name, table1.description, 12, UNIX_TIMESTAMP()
  FROM GiveArang_categories Where `p_id`= '225' ");

I need to insert data into the second database which is selected from the first database. Both databases have different connections. It needs to done in php in a single query .

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
Mari Muthu
  • 1
  • 1
  • 1
  • You can't pass two link_identifiers at the same time. So I think it's pretty impossible. Well, it's just me. – Leri Jul 07 '12 at 11:35

4 Answers4

1

The only way of doing this (that I know of) is by two separate connections.

Something like this:

mysql_connect("localhost","root","");
mysql_select_db("database2");
mysql_query("INSERT INTO database2.categories (category_name,description,group_id,created)");
mysql_close();

mysql_connect("localhost","root","");
mysql_select_db("database1");
mysql_query("SELECT table1.name, table1.description, 12, UNIX_TIMESTAMP() FROM GiveArang_categories Where `p_id`= '225' ");
mysql_close();
Secko
  • 7,664
  • 5
  • 31
  • 37
  • This is Working mysql_connect($host1,$username1,$password1); mysql_select_db($db_name1); $cat_name=mysql_query("SELECT table1.name FROM table1 Where `p_id`= '1234' "); if(mysql_num_rows($cat_name)) { while($var = mysql_fetch_array($cat_name)){ mysql_connect($host2,$username2,$password2); mysql_select_db($db_name1); mysql_query("INSERT INTO table2.name2 (description,gid,created) values ('". $var['name']."','". $var['name']."','15',UNIX_TIMESTAMP())"); } } mysql_close(); – Mari Muthu Jul 09 '12 at 12:25
0

Unfortunately there's no Oracle dblink (which is what you need to connect to another database in a single query in Oracle Database) equivalent in MySql.

See also this for further information.

Community
  • 1
  • 1
Vincenzo Maggio
  • 3,787
  • 26
  • 42
  • Oracle has a functionality called dblink which let a single query to connect to different databases with different connections, which is the exact feature he would need in MySql – Vincenzo Maggio Jul 07 '12 at 11:36
0

You can pop a MySQL Proxy to sit between your server and database boxes which will allow you to treat them as if they are on the same server. You can then use databasename.tablename within your query to identify your different databases via the same connection.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
0

If both databases are on the same mysql-server you can note it like this:

mysql_connect("localhost","root","");
mysql_query("INSERT INTO database2.categories (category_name,description,group_id,created)
  SELECT database1.table1.name, database1.table1.description, 12, UNIX_TIMESTAMP()
  FROM database1.GiveArang_categories Where database1.GiveArang_categories.p_id= '225' ");
David
  • 5,882
  • 3
  • 33
  • 44