dongluan0020 2019-04-29 10:37
浏览 26

查询数据库1,找到匹配的行并更新到数据库2中

I have 2 separate databases. Both contain a column called "UID". I need to scan each row in database 1 and 2 and if "UID" matches then update database 2 with the rest of the row's information. Please advise the best way to go about this in PHP. I do have code an so far testing fine but want to be sure this is the best way to go about it

//database 1 query 
$query_db1 = mysqli_query($conn_db1, "SELECT *
FROM `table`.`list`
WHERE `list`.`reg_date` >= DATE_SUB(NOW(),INTERVAL 24 HOUR)");

$counter = 0;
while ($row_db1 = mysqli_fetch_assoc($query_db1)) {

  //find records, compare uid, if found then update status
  //prepare update query into database 2
  $stmt = $conn_db2->prepare("UPDATE TABLE SET Disposition=? WHERE UID=?");
  $stmt->bind_param("ss", $row_db1['status'], $row_db1['UID']);

    if ($stmt->execute()) {
    echo "success, "; 
    if($stmt->affected_rows === 1){
      $counter++;
      }

   } else {
        echo "Error connecting to database";
  }

}

echo "Rows updated: $counter <br>";

$stmt->close();
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
    • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
    • ¥15 Google speech command 数据集获取
    • ¥15 vue3+element-plus页面崩溃
    • ¥15 像这种代码要怎么跑起来?
    • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
    • ¥15 pyqt5tools安装失败
    • ¥15 mmdetection
    • ¥15 nginx代理报502的错误
    • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧