php how to insert query result from server 1 to server 2 table in mySQL php

Go To StackoverFlow.com

1

I'm new to PHP first of all ..

My question is once I got the $result from Server1 using mysql_query , how can I create Table2 that would stored in host2

<?php

$connect1 = mysql_connect(host1,user,pass);
$selected = mysql_select_db(database1,$connect1);

$result = mysql_query("select a,b,c from table1");

$connect2 = mysql_connect(host2,user,pass);
$selected = mysql_select_db(database2,$connect2);

mysql_query("create table table2 as select $result from table2"); 

?> 

They are two different servers (both in MySQL) .. answer in details would appreciated.

Thanks

2012-04-05 00:05
by JPC
Let me caution you to use either the <code>mysqli</code> extension or <code>PDO</code> for database operations. mysql is a dinosaur that's on the way out. You'll be much better served using one of the other methods I've mentioned - rdlowrey 2012-04-05 00:19
I agree with @rdlowrey. My answer below works just the same with mysqli and it is even simpler with PDO. PDO is usually my connection of choice - gokujou 2012-04-05 00:28
Thanks for the fast response, Can you show me how to do PDO on this similar purpose ? I would really appreciated it - JPC 2012-04-05 15:19


1

mysql_query accepts two parameters. The second is the connection, have you tried this?

mysql_query("create table table2 as select $result from table2", $connect2);

PHP Documentation on mysql_query: http://php.net/manual/en/function.mysql-query.php

2012-04-05 00:13
by gokujou
Ads