douluoxiao2286 2014-11-11 12:21
浏览 63
已采纳

使用从while循环检索的数据更新数据库

I Have a table named FRANCE like this

  City          Region            LAT   LNG
  PARIS         L'Ile-de-France  
  MARSEILLE     Provenza

Now, LAT and LNG values I retrieve throught a function that use google api. To do this I must concatenate City and Region

So this is what I do:

$sql="Select * from France";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result)){
$city=$row['city'];
$region=$row['region'];

 $address=$city.",".$region.", France";

$coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' .      urlencode($address) . '&sensor=true');
$coordinates = json_decode($coordinates);

$lat = $coordinates->results[0]->geometry->location->lat;
$lng = $coordinates->results[0]->geometry->location->lng;
 }

Now I'd like to update the table FRANCE to have this output

  City          Region            LAT         LNG
  PARIS         L'Ile-de-France   48.856614   2.352222
  MARSEILLE     Provenza          43.296482   5.369780

How can I do?

  • 写回答

1条回答 默认 最新

  • duaeim2874 2014-11-11 12:31
    关注

    It's quite straightforward, just like the comments above, you already got the fetching of results and made the request. After gathering the response from the request just make that UPDATE statement.

    Obligatory note:

    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

    Here is a rough untested example using mysqli with prepared statements. Of course you need to modify those items:

    // first step connect and make the first query
    $db = new mysqli('localhost', 'username', 'password', 'database');
    $sql = 'SELECT * FROM france';
    $query = $db->query($sql);
    
    while($row = $query->fetch_assoc()) {
        // fetch and assign results
        $id = $row['id'];
        $city = $row['city'];
        $region = $row['region'];
        $address = $city.",".$region.", France";
    
        // make the google request and gather results
        $coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true');
        $coordinates = json_decode($coordinates);
    
        // check if there are results
        if($coordinates != null) {
            $lat = $coordinates->results[0]->geometry->location->lat;
            $lng = $coordinates->results[0]->geometry->location->lng;
    
            // make the update
            $sql2 = 'UPDATE france SET `LAT` = ?, `LNG` = ? WHERE id = ?';
            $update = $db->prepare($sql2);
            // i don't know if this is double column or varchar
            $update->bind_param('ddi', $lat, $lng, $id);
            $update->execute();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)