dongsonglian7303 2013-11-20 07:05
浏览 39
已采纳

从数组字段更新表 - 代码使用最后一个值填充所有字段

SOLVED. SEE BELOW.

Initial question:

I need to extract lat and lon from an array field (643844X2X48) in a mySQL table where the data look like this:

56.559482483762245;-104.0625;;SK;CA;S0J

I tried mysqli and pdo code and failed.

This php/mysql code updates all of the lat fields with the final loop lat value:

$sql = "SELECT 643844X2X48 from `reg_survey_643844`";
$array = mysql_query($sql);
while($row=mysql_fetch_assoc($array)){
foreach($row as $value){
    $result= explode(";", $value);
    $slice= (array_slice($result,0,1)); 
    $lat = array_shift(array_values($result));
    mysql_query("UPDATE reg_survey_643844 SET lat = '$lat'");
    }
    }

What am I doing wrong?

UPDATE:

Here is the working code.

$query = "SELECT * FROM reg_survey_643844 ORDER BY id";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
        $resultGeo= explode(";", $row["643844X2X48"]);
        $lat = $resultGeo[0];
        $lon = $resultGeo[1];
        $city = $resultGeo[2];
        $state = $resultGeo[3];
        $countryCode = $resultGeo[4];
        $zip = $resultGeo[5];
        $getid = $row['id'];
        $siteName = $row['643844X2X12'];

        $query = mysql_query("UPDATE reg_survey_643844 
                        SET siteName = '$siteName', lat = '$lat', lon = '$lon', city = '$city', state = '$state', countryCode = '$countryCode', zip = '$zip'
                   WHERE id = '$getid'");

        }
  • 写回答

1条回答 默认 最新

  • dongying9712 2013-11-20 07:12
    关注

    Try this

     $sql = "SELECT 643844X2X48 from `reg_survey_643844`";
        $array = mysql_query($sql);
        while($row=mysql_fetch_assoc($array)){
    
        $result= explode(";", $row["your data"]);
    
        $lat = $result[0];
        mysql_query("UPDATE reg_survey_643844 SET lat = '$lat' whrere  your conditions");
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式