数据库中的LONG/LAN地理位置 [英] Geolocation LONG/LAN in database

查看:65
本文介绍了数据库中的LONG/LAN地理位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了php代码,将地址添加到数据库中.然后,我编写了一个脚本来读取该地址中的long/lan,并将其存储在DB中.目前数据库中有10个项目,但是当我添加第12或第13个项目时,它将一次又一次地循环遍历整个数据库以设置所有long/lan.我当时想对de DB中的行进行计数,然后在更新查询中我只写了update ...其中ID = $ rowCount 这是我的代码,但是我收到一个错误.尝试在第146行中获取非对象的属性:$ row_cnt = $ result-> num_rows;

I wrote php code to add adresses to a database. Then I wrote a script to read the long/lan from this adressen and store them in the DB. At this moment there are 10 items in the DB but when i add the 12th or 13th, it loops the whole database again and again to set all this long/lan. I was thinking to count the rows in de DB and then in the update query i just wrote update ... where ID = $rowCount This is my code but i receive an error. Trying to get property of non-object in line 146: $row_cnt = $result->num_rows;

    public function saveLongLan()
    {
        define("MAPS_HOST", "maps.google.com");
        define("KEY", "MYKEY");

        $link = mysql_connect('localhost', 'root', 'root');
        if (!$link) {
         die('Not connected : ' . mysql_error());
        }

        // Set the active MySQL database
        $db_selected = mysql_select_db('Foodsquare', $link);
        if (!$db_selected)  
        {
            die("Can\'t use db : " . mysql_error());
        }

        $query = "SELECT * FROM tblPlaces WHERE 1";
        $result = mysql_query($query);
            if (!$result) 
                {
                    die("Invalid query: " . mysql_error());
                }

        $row_cnt = $result->num_rows;
        $delay = 0;
        $base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;

        while ($row = @mysql_fetch_assoc($result)) 
            {
                $geocode_pending = true;

                    while ($geocode_pending) 
                        {
                            $address = $row["Street"];
                            $id = $row["Id"];
                            $request_url = $base_url . "&q=" . urlencode($address);
                            $xml = simplexml_load_file($request_url) or die("url not loading");

                            $status = $xml->Response->Status->code;
                            if (strcmp($status, "200") == 0) 
                                {
                                      // Successful geocode
                                      $geocode_pending = false;
                                      $coordinates = $xml->Response->Placemark->Point->coordinates;
                                      //explode functie breekt een string af vanaf een bepaald teken en stopt hem dan in een array
                                      $coordinatesSplit = explode(',', $coordinates);
                                      // formaat: Longitude, Latitude, Altitude
                                      $lat = $coordinatesSplit[1];//getal = plaats in array
                                      $lng = $coordinatesSplit[0];

                                      $query = sprintf("UPDATE tblPlaces".
                                         " SET lat = '%s', lng = '%s' " .
                                         " WHERE Id = '" . $row_cnt . "' LIMIT 1;",
                                         mysql_real_escape_string($lat),
                                         mysql_real_escape_string($lng),
                                         mysql_real_escape_string($id));
                                      $update_result = mysql_query($query);
                                          if (!$update_result) 
                                            {
                                            die("Invalid query: " . mysql_error());
                                            }
                                } else if (strcmp($status, "620") == 0) 
                                    {
                                          // sent geocodes too fast
                                          $delay += 100000;
                                    } 
                                    else 
                                    {
                                        // failure to geocode
                                        $geocode_pending = false;
                                        echo "Address " . $address . " failed to geocoded. ";
                                    echo "Received status " . $status . " \n";
                                    }
                                usleep($delay);
                        }
            }

    }

}

我尚未在更新语句中进行任何更改.

I didn't change anything in the update statement yet.

推荐答案

您是要更新数据库中已有的值还是要完全添加新值,如果是后者,我建议尝试使用INSERT INTO <table_name> <Values>而不是UPDATE似乎对我来说一直很好,希望这对我有帮助!

Are you trying to update values already in the database or add new values entirely, if it's the latter I would suggest trying INSERT INTO <table_name> <Values> as opposed to UPDATE that always seemed to work fine for me, hope this helps !!

这篇关于数据库中的LONG/LAN地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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