Insert into two php mysql database

Asked

Viewed 562 times

-2

I want to insert data from a register in two databases at the same time using PHP/MYSQL.

Is there any possibility? .

I’m just getting through a connection, when I do the require of two connections nothing happens!

I need an example of code friends

1 answer

4

If it’s on the same server:

$con = mysqli_connect( 'end.do.servidor', 'usuario', 'senha ' );

if( !$con ) die( 'Falha na conexao: '.mysqli_connect_error( $con ) );

$sql1 = 'INSERT INTO nomedobanco1.tabela1 (....)';
$sql2 = 'INSERT INTO nomedobanco2.tabela2 (....)';

if( mysqli_query( $con, $sql1 ) ) echo 'Inserção 1 OK' else die( mysqli_error( $con ) ); 
if( mysqli_query( $con, $sql2 ) ) echo 'Inserção 2 OK' else die( mysqli_error( $con ) ); 

If they’re different servers:

$con1 = mysqli_connect( 'end.do.servidor1', 'usuario', 'senha ' );
$con2 = mysqli_connect( 'end.do.servidor2', 'usuario', 'senha ' );

if( !$con1 ) die( 'Falha na conexao 1: '.mysqli_connect_error( $con1 ) );
if( !$con2 ) die( 'Falha na conexao 2: '.mysqli_connect_error( $con2 ) );

$sql1 = 'INSERT INTO nomedobanco1.tabela1 (....)';
$sql2 = 'INSERT INTO nomedobanco2.tabela2 (....)';

if( mysqli_query( $con1, $sql1 ) ) echo 'Inserção 1 OK' else die( mysqli_error($con1 ) );
if( mysqli_query( $con2, $sql2 ) ) echo 'Inserção 2 OK' else die( mysqli_error($con2 ) );

For PDO, the logic is the same, just adapt the syntax.

If you are using only one connection, and one engine like Innodb, you can use a BEGIN TRANSACTION before 2 INSERTs and a COMMIT then to ensure that the transaction is only effective if all goes well on the two inserts, avoiding that only one table is modified. If you’re using two connections, you need to think of another solution to ensure consistency.

  • I’m gonna check it out, buddy, I appreciate it

  • it is possible to have several database connections using this example

  • using a require or include?

  • 2

    Require and include do not influence anything in the database, they are just functions for code organization. For PHP it is virtually indifferent whether it is all in one code or whether it was included. The number of connections regardless of that too, can be in include, separate function, class, etc..

  • then Bacco can enter here in my file of connection.php and feed my two database without problem right right?

  • As you did not give details on the question, please activate me to the problem of connections. There is no problem for several connections in include. What you need to pay attention to is if you miss one of the inserts, you need to address this in the logic of your application (read what I wrote about transactions). As for putting the connections in include, no problem at all. Even, if they are always the same table names, you can create a function that already inserts in all, instead of repeating code. Run away a little from what was asked, but it’s good you after testing everything right, think about the idea.

  • https://i.stack.Imgur.com/lgfus.png

  • made a strange mistake

  • Probably because you did not use the syntax indicated in the answer. You are passing a string where the connection should be.

  • Bacco is very correct, just duplicate the connection by changing its name and data(server, base, user, Psw...), then you do two Inserts one with the conn1 in table x and the other with the conn2 in table y, using the same data already obtained in PHP.

Show 5 more comments

Browser other questions tagged

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