How do I insert variables from a select query into a database on a different server?

-4

I would like to make a connection in php on two databases only on different networks, in that connection I would need to search a database on a server and insert it into a table in a database on another server

For example:

$conecta = mysql_connect("rede1","usuario","senha");
$db = mysql_select_db("banco1");

and put a second connection:

$conecta1 = mysql_connect("rede2","usuario","senha");
$db = mysql_select_db("banco2");

// Here is where I do the query to fetch the data that will be stored in the variables

$serial = $_POST['serial'];

$query = mysqli_query("select coluna1 coluna2... from tabela1 where coluna 1 = '$serial'");

$result = mysqli_fecth_array($query);

$qry = $result['coluna2'];

For example:

$sql = mysqli_query($conecta,"select * from tabela1");
$result = mysqli_fetch_array($sql);

and with result of this query it makes an insert in a table in the database in the other server

$insert = mysqli_query(conecta1."insert into tabela2 values ('resultado da query acima')");

I know you can insert with the select inside for example:

$insert = mysqli_query($conecta1."insert into tabela2(".."select '$qry'... from tabela1) on duplicate key update coluna2 = values(coluna2)...");

I need to populate a database table from such a server and this padding will be the one from a select query where the data will be stored in a variable from a first select query

I did it this way but it did not work, does anyone know if there is a way to do this in php ? since then thank you.

    
asked by anonymous 22.03.2016 / 17:01

1 answer

1

At this point in the competition you should not be using mysql_ *, it has been deprecated, php7 no longer works, you should be using pdo, mysqli_ or some framework.

But if you insist on using mysql _

$conecta1 = mysql_connect("rede1","usuario1","senha1");
mysql_select_db("banco1", $conecta1);

$conecta2 = mysql_connect("rede2","usuario2","senha2");
mysql_select_db("banco2", $conecta2);

mysql_query('select * from tablenamebanco1', $conecta1);
mysql_query('select * from tablenamebanco2', $conecta2);

If you do not pass the handle at the end the last created connection will be used.

    
24.03.2016 / 04:36