How to update a table from one server to another?

◇◆丶佛笑我妖孽 提交于 2019-12-10 12:16:50

问题


I need to do an update from one server to another and could only get to the code that are below. I wanted to know where the mistake is.

If separating the codes works perfectly.

<?PHP
$db_host1     = "10.0.0.101";
$db_user1     = "dns";
$db_password1 = "123456";
$db_name1     = "dns";
$db_connect1  = mysqli_connect($db_host1, $db_user1, $db_password1, $db_name1);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$db_host2     = "10.0.0.102";
$db_user2     = "dns";
$db_password2 = "123456";
$db_name2     = "dns";
$db_connect2  = mysqli_connect($db_host2, $db_user2, $db_password2, $db_name2);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($db_connect2,"UPDATE ipserver SET ipsserver = ($db_connect1,"SELECT ipsserver FROM ipserver WHERE idserver = '11'") WHERE idserver = '11'");

mysqli_close($db_connect1);
mysqli_close($db_connect2);
?>

Thanks to @Jon Dinham for helping me solve my problem

I'll put the script already corrected

<?PHP
$db_host1     = "10.0.0.101";
$db_user1     = "dns";
$db_password1 = "123456";
$db_name1     = "dns";
$db_connect1  = mysqli_connect($db_host1, $db_user1, $db_password1, $db_name1);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$db_host2     = "10.0.0.102";
$db_user2     = "dns";
$db_password2 = "123456";
$db_name2     = "dns";
$db_connect2  = mysqli_connect($db_host2, $db_user2, $db_password2, $db_name2);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query(
$db_connect1,
"SELECT ipsserver FROM ipserver WHERE idserver='11'"
);

$rows = mysqli_fetch_array($result,MYSQLI_NUM);

foreach ($rows as $index=>$row) {
$ipsserver = $row;

mysqli_query(
$db_connect2,
"UPDATE ipserver SET ipsserver='$ipsserver' WHERE idserver='11'"
);
}

mysqli_close($db_connect1);
mysqli_close($db_connect2);
?>

回答1:


You should set up MySQL replication (master/slave) and execute the queries on the master DB only. See documentation: http://dev.mysql.com/doc/refman/5.1/en/replication-howto.html

For just 1 time querying, replace the line:

mysqli_query($db_connect2,"UPDATE ipserver SET ipsserver = ($db_connect1,"SELECT ipsserver FROM ipserver WHERE idserver = '11'") WHERE idserver = '11'");

with this code:

$result = mysqli_query(
  $db_connect1,
  "SELECT ipsserver FROM ipserver WHERE idserver='11'"
);

while ($row=$result->fetch_assoc()) {
  $ipsserver = $row["ipsserver"];

  mysqli_query(
    $db_connect2,
    "UPDATE ipserver SET ipsserver='$ipsserver' WHERE idserver='11'"
  );
}


来源:https://stackoverflow.com/questions/25497503/how-to-update-a-table-from-one-server-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!