我编写了PHP代码来向数据库添加地址。然后我编写了一个脚本来读取此adressen中的long / lan并将它们存储在DB中。此时数据库中有10个项目,但是当我添加第12个或第13个项目时,它会一次又一次地循环整个数据库以设置所有这些长/局域网。我正在考虑计算de DB中的行,然后在更新查询中我刚写了update ...其中ID = $ rowCount 这是我的代码但我收到一个错误。试图在第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);
}
}
}
}
我还没有在update语句中更改任何内容。
答案 0 :(得分:1)
您是否正在尝试更新数据库中已有的值或完全添加新值,如果是后者我会建议尝试INSERT INTO <table_name> <Values>
而不是UPDATE
这对我来说似乎总是正常工作,希望这有助于!!