我正在尝试从一个数据库中选择值。并将结果插入并更新到另一个。这是cronjob,需要每天运行以将一些数据从一个数据库复制到另一个数据库。我知道我缺少步骤/正确的语法,但我希望有人可以帮助我。
<?php
$con_1=mysqli_connect("host","user","pw","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$con_2=mysqli_connect("host","user","pw","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con_1,"SELECT id, name FROM table GROUP BY 1,2");
$mysqli->query($con_2, "INSERT INTO `table2`(`id`, `name`) VALUES ('".$result[1]."', ".$result[2].")
ON DUPLICATE KEY UPDATE name = ".$result[2]."");
}
mysqli_close($con_1);
mysqli_close($con_2);
?>
答案 0 :(得分:1)
mysqli_query
返回一个查询对象,使用$result[1]
没有意义,你需要在循环中获取行:
while($row = $result->fetch_assoc()) {
// insert result in second database
}
对于其他访问方法,请检查documentation。