使用数组中的php循环更新mysql db [英] update mysql db with php loop from array

查看:101
本文介绍了使用数组中的php循环更新mysql db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用循环更新mysql中的一列.我有每4小时从另一个数据库中提取两个数组.第一个在我的数据库中有一个站号作为索引.第二个是我要更新的数据.

I want update a column in mysql with a loop. I have two arrays that are pulled from another database every 4 hours. 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]."'")

运行某种循环并一次更新所有内容似乎更有意义.但是我不确定如何在每个循环中增加数组索引的数量.不幸的是,我似乎对php的了解达到了顶峰.当然是我的耐心,因为我花了一天的时间来解决这个问题.

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. Certainly my patience, as I have spent a day trying to figure this out.

推荐答案

我不理解您描述数据的方式,但是如果我基于已经进行的调用,我可以认为这会起作用: /p>

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

这篇关于使用数组中的php循环更新mysql db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆