Jump to content

Transfering data over different servers.


OsirisElKeleni

Recommended Posts

Hello everybody!

 

I'm here today with a mystery i can't solve...

The problem i'm having is that i need to be able to transfer mysql data from one server to another one.

Retrieving data from the one database is easy, but making it transfer to another server is not that easy for me.

I have it printing out the data ATM

 

This is how i retrieve data from one server:

<?php 
$dbhost = "";
$dbname = "eg_xenforo";
$dbuser = "";
$dbpass = "";
$conn = mysqli_connect("$dbhost","$dbuser","$dbpass","$dbname") or die("Error " . mysqli_error($conn)); 

$sql=	"SELECT username
		FROM xf_user
		WHERE FIND_IN_SET('9',secondary_group_ids)
        UNION
		SELECT username
		FROM xf_user
		WHERE FIND_IN_SET('10',secondary_group_ids)
        UNION
		SELECT username
		FROM xf_user
		WHERE FIND_IN_SET('12',secondary_group_ids)";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
		$name = $row['username'];
        echo "$name <br>";
    }
} else {
    echo "0 results";
}

?>

This is what i get as result:

CosmonautBob 
ooglebrain 
JamieKG 
Quinnter16 
Jordan 
Zoehamp 
skynet1123 
misscupcake1306 
Sir Crimson

These are usernames that should be added onto another server in a database table.

Any suggestions?

 

Thanks in advance!

Link to comment
Share on other sites

Depends on the second server, really. Does it have an API? Or some script that can insert the data to that second server's database?

 

You could just convert the data selected from server1 into an array and send that as the body in a POST(or PUT) request to server2 provided server2 has a script to handle that.

Link to comment
Share on other sites

Thanks for the quick response guys!

 

However how will i do this? can i connect to another server in the same script?

Store the data in an array then connect to the second server and write the data

 

The second server is a mysql database as well if that is what you mean.

If you could give me a little start to what functions i should use i can work my way from there i think.

Depends on the second server, really. Does it have an API? Or some script that can insert the data to that second server's database?

 

You could just convert the data selected from server1 into an array and send that as the body in a POST(or PUT) request to server2 provided server2 has a script to handle that.

 

Thanks!

Link to comment
Share on other sites

The way I see it you have two options:

 

1. Send a request with the data you want inserted to server2

2. After getting the data from server1 (your result), you setup another Database connection to the database on server2

 

Personally I would opt for the second option, if that is available to you. Then you could simply loop over your results. Pardon me if the code example looks a bit iffy, I genuienly don't use mysqli, but I'm sure you'll get the idea.

 

 

$databaseServer2 = mysqli_connect($host, $user, $pass, $db);
while($row = mysqli_fetch_assoc($result)) {
$name = $row['username'];
$databaseServer2->query('INSERT INTO table(name) VALUES($name)');
}

 

 

As long as I understood what you wanted to do correctly, then this should work

Link to comment
Share on other sites

The way I see it you have two options:

 

1. Send a request with the data you want inserted to server2

2. After getting the data from server1 (your result), you setup another Database connection to the database on server2

 

Personally I would opt for the second option, if that is available to you. Then you could simply loop over your results. Pardon me if the code example looks a bit iffy, I genuienly don't use mysqli, but I'm sure you'll get the idea.

$databaseServer2 = mysqli_connect($host, $user, $pass, $db);
while($row = mysqli_fetch_assoc($result)) {
$name = $row['username'];
$databaseServer2->query('INSERT INTO table(name) VALUES($name)');
}

As long as I understood what you wanted to do correctly, then this should work

Alright i think you're saying its possible to connect to 2 different servers in one script?

I've looked up on this and i found this:

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Do you know for sure its possible to connect to 2 different servers at once?

If so i'd love to have a confirmation on that because it all seems not possible ;p

Link to comment
Share on other sites

Rather than have a loop running the inserts a multiple insert would be fare more efficient, eg

INSERT INTO table2 (username) VALUES
('CosmonautBob'), ('ooglebrain'),('JamieKG'),('Quinnter16')

So your code would be

$names = array();
while($row = mysqli_fetch_assoc($result)) {
        $name = $row['username'];
        $names[] = "('$name')";
    }

$conn->close();
        //// now connect to the second server
$conn2 = new mysqli(HOST2,USERNAME,PASSWORD,DATABASE);
$sql = "INSERT INTO table2 (username) VALUES\n" . join(',', $names);
$conn2->query($sql);
Edited by Barand
Link to comment
Share on other sites

Alright i think you're saying its possible to connect to 2 different servers in one script?

I've looked up on this and i found this:

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Do you know for sure its possible to connect to 2 different servers at once?

If so i'd love to have a confirmation on that because it all seems not possible ;p

 

 

Of course. You can connect to however many servers you want from a script.

 

The thing you have to take in mind is configure the firewalls so that server2 accepts connections from server1, for instance. And also make sure the mysql user is allowed to come from server1 to server2. After that, you should be fine.

Link to comment
Share on other sites

If you have the proper credentials to connect to the second domain's database then it is pretty simple I would think.  First you connect to your db server (db), grab some data, close the conn and open a new conn on the other db server and insert that data.

 

Of course I haven't done this but I'm sure that Barand will guide you thru this if this approach has some pitfall to it.

Link to comment
Share on other sites

The data will be lost if you close the connection, yes.  But you simply need to do a fetchall to pull it into your own local array variable to retain it.

 

(not sure but if you don't close the conn but simply open the second new conn with a new variable you might hold on to the data)

Link to comment
Share on other sites

If, as I suggested in reply #7, your data is stored in an array the data will not be lost. Only data in any unsaved result sets will be lost when the connection closes.

 

There is no problem with two connections. I have done it many times when I needed to connect to a development and production server in the same script or when a script required connections to MySql server and an MS SQL server

Link to comment
Share on other sites

If, as I suggested in reply #7, your data is stored in an array the data will not be lost. Only data in any unsaved result sets will be lost when the connection closes.

 

There is no problem with two connections. I have done it many times when I needed to connect to a development and production server in the same script or when a script required connections to MySql server and an MS SQL server

Okido!

I will try it and get back to you with results or errors i can't solve thanks a lot!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.