I need to get mysql data from one server(host1) and store in to another server(host2).
Here is my try.
<?php
$servername = "host1";
$username = "root";
$password = "";
$dbname = "userdetails";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$sql = "SELECT ts, a, logic,status,rel,MAX(ts) OVER (PARTITION BY a) AS LastUpdated FROM block_queue_alltime order by ts";
$sql = "SELECT a,logic from usertable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$a=array();
$logic=array();
while($row = $result->fetch_assoc()) {
$a[]=$row["a"];
$logic[]=$row["logic"];
}
} else {
echo "0 results";
}
$servername2 = "host2";
$username2 = "root";
$password2 = "";
$dbname2 = "userdetails2";
$conn2 = mysqli_connect($servername2, $username2, $password2, $dbname2);
if (!$conn2) {
die("Connection failed: " . mysqli_connect_error());
}
$sql2 = "INSERT INTO testdata (number, logic) VALUES ('$a', '$logic')";
$result2 = mysqli_query($conn2, $sql2);
if ($result2 === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
mysqli_close($conn);
?>
I can see when print_r($a) and print_r($logic) provides the required array of numbers what I need. But when it inserting to another the table. It shows only zeros in the databases and New record created successfully
also prints. Can someone show me an efficient way to store all the data from one host to another?