本文介绍了如何将表从一台服务器更新到另一台服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一台服务器更新到另一台服务器,并且只能访问下面的代码.我想知道错误在哪里.

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);
?>

感谢@Jon Dinham帮助我解决了问题

Thanks to @Jon Dinham for helping me solve my problem

我将已更正的脚本

<?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);
?>

推荐答案

您应该设置MySQL复制(主/从)并仅在主数据库上执行查询.参见文档: http://dev.mysql.com/doc/refman/5.1/en/replication-howto.html

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

仅查询1次,请替换以下行:

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'");

使用以下代码:

$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'"
  );
}

这篇关于如何将表从一台服务器更新到另一台服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 06:07