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

Asked

Viewed 651 times

-4

I would like to make a connection on php in two databases only that are in different networks, in this connection would need to do a search in a database on a server and insert in 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 as a result of this query makes an Insert in a table in the database on the other server

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

i know you can do the Insert with 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 fill a database table of such a server and this fill will be coming from a query select, where the data will be stored in a variable coming from a first query select

I did it that way but it didn’t work, someone knows if it exists in php a way to do it ? since then I thank you.

  • 1

    This is a matter of network and not PHP.

  • I couldn’t use one comic at a time?

1 answer

1

At this point in the competition you shouldn’t be using mysql_*, it’s depreciated, 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 identifier at the end the last connection created will be used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.