简体   繁体   English

用数组中的php循环更新mysql数据库

[英]update mysql db with php loop from array

I want update a column in mysql with a loop. 我想用循环更新mysql中的一列。 I have two arrays that are pulled from another database every 4 hours. 我有两个数据,每4小时从另一个数据库中提取。 The first has a station number that is in my db as an index. 第一个站号在我的数据库中作为索引。 The second is the data I want to update. 第二个是我想要更新的数据。

    $stano = 13235000,13236500,13237920
    $nt = 379,49,131

Currently I simply update with a bunch of queries: 目前我只是用一堆查询更新:

 mysql_query("UPDATE gages SET cfs = '".$nt[3][2]."' WHERE sgs = '".$stano[3][1]."'")
 mysql_query("UPDATE gages SET cfs = '".$nt[4][2]."' WHERE sgs = '".$stano[4][1]."'")
 mysql_query("UPDATE gages SET cfs = '".$nt[5][2]."' WHERE sgs = '".$stano[5][1]."'")

It seems it would make more sense to run some kind of loop and update all in one shot. 看起来运行某种循环并一次更新所有内容会更有意义。 But I'm not sure how to step up the number of the array index with each loop. 但我不知道如何用每个循环增加数组索引的数量。 Unfortunately, I seem to have reached my ceiling of understanding with php. 不幸的是,我似乎已经达到了我对php的理解上限。 Certainly my patience, as I have spent a day trying to figure this out. 当然是我的耐心,因为我花了一天的时间试图解决这个问题。

I don't understand the way you describe your data, but if i base myself on the calls you make already, i can suppose that this will work: 我不明白你描述你的数据的方式,但如果我基于你已经进行的调用,我可以认为这将有效:

for($i = 0; $i < count($nt); $i++) {
  mysql_query("UPDATE gages SET cfs = '".$nt[$i][2]."' WHERE sgs = '".$stano[$i][1]."'");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM