2012-05-11 6 views
0

データベースに住所を追加するためのPHPコードを書いています。それから、このアドレスからlong/lanを読み込んでDBに格納するスクリプトを書いた。この時点でDBには10個のアイテムがありますが、12番目または13番目のアイテムを追加すると、データベース全体が何度も繰り返しループし、この長い/ lanが設定されます。私はDBの行を数えると思っていたし、更新クエリで私はちょうど更新を書きました... ID = $ rowCount これは私のコードですが、エラーが表示されます。行146の非オブジェクトのプロパティを取得しようとしています:$ row_cnt = $ result-> num_rows;ジオロケーションデータベース内のLONG/LAN

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); 
         } 
      } 

    } 

} 

私はまだ更新ステートメントで何も変更しませんでした。

答えて

1

UPDATEとは対照的に、後者の場合はINSERT INTO <table_name> <Values>を試してみることをお勧めします。データベースに既に値を追加したり、新しい値を追加しようとしていますか?

+0

更新は問題ではなく、レコードが保存されるたびに、データベース全体がlan/longで更新されることを意味しました。更新ステートメントではなく、このステートメントにはエラーはありません。 $ row_cnt = $ result-> num_rows; – Niels

+0

num_rowsは配列内の要素ですか、それともこのリンクが使用できるオブジェクトのプロパティですか? :[リンク](http://stackoverflow.com/questions/5175161/trying-to-get-property-of-non-object-codeigniter)http://stackoverflow.com/questions/5175161/trying-to-get -property-of-non-object-codeigniter [リンク] – scrineym

+0

$ row_cnt = mysqli_num_rows($ result);私はこれでこれを置き換え、それは動作しますが、今私は私の次のエラーが表示されます。 mysqli_num_rows()は、パラメータ1がmysqli_resultであると想定します。私はこれは私の更新ステートメント – Niels