2012-03-09 25 views
0

我能夠從網站(Nestoria)檢索數據並存儲到我的PostGIS數據庫中。然而,並非每個返回的單個結果都具有經度和緯度值,因此它們不會存儲到數據庫中。我嘗試使用(pg_escape_string),以便它可以將空值作爲雙引號(「)返回,但它不起作用。數據庫中我的緯度和長度字段的字段類型是」雙精度「 這是一個示例輸出的錯誤:(「警告:pg_query()[function.pg-query]:查詢失敗:錯誤:類型雙精度輸入語法無效:」「線1: ..', '', '')^在C:\ XAMMP ... \ database.php中第12" 行)轉義空值(Nestoria英國)

請參閱下面的代碼檢索數據:

<?php 

$url = ("http://api.nestoria.co.uk/api?action=search_listings&centre_point=51.5424,-0.1734,2km&listing_type=rent&property_type=all&price_min=min&price_max=max&bedroom_min=0&bedroom_max=0&number_of_results=50&has_photo=1&page=4"); 

$xml = simplexml_load_file($url); 

foreach ($xml->response->listings as $entry) { 

echo $entry->attributes()->title; 
echo $entry->attributes()->bathroom_number; 
echo $entry->attributes()->bedroom_number; 
echo $entry->attributes()->datasource_name; 
echo $entry->attributes()->guid; 
echo $entry->attributes()->img_url; 
echo $entry->attributes()->keywords; 
echo $entry->attributes()->lister_name; 
echo $entry->attributes()->listing_type; 
echo $entry->attributes()->price; 
echo $entry->attributes()->price_type; 
echo $entry->attributes()->property_type; 
echo $entry->attributes()->summary; 
echo $entry->attributes()->latitude; 
echo $entry->attributes()->longitude; 

// Process XML file 

} 
?> 

查找低於將值存儲到數據庫中的代碼:

<?php 
require 'nestoriauk.php'; 

// Opens a connection to a PostgresSQL server 
$connection = pg_connect("dbname=postgis user=postgres password=xxxx"); 

// Execute query   

foreach ($xml->response->listings as $entry) { 
$query = "INSERT INTO nestoriaphp(title, bathroom, bedroom, datasource, guid, image, keywords, lister, listype, price, pricetype, property_type, summary, latitude, longitude) VALUES ('" . pg_escape_string($entry->attributes()->title) . "', '" . pg_escape_string($entry->attributes()->bathroom_number) . "', '" . pg_escape_string($entry->attributes()->bedroom_number) . "', '" . pg_escape_string($entry->attributes()->datasource_name) . "', '" . pg_escape_string($entry->attributes()->guid) ."', '" . pg_escape_string($entry->attributes()->img_url) . "', '" . pg_escape_string($entry->attributes()->keywords) . "', '" . pg_escape_string($entry->attributes()->lister_name) . "', '" . pg_escape_string($entry->attributes()->listing_type) . "', '" . pg_escape_string($entry->attributes()->price) . "', '" . pg_escape_string($entry->attributes()->price_type) . "', '" . pg_escape_string($entry->attributes()->property_type) ."', '" . pg_escape_string($entry->attributes()->summary) . "', '" . pg_escape_string($entry->attributes()->latitude) . "', '" . pg_escape_string($entry->attributes()->longitude) . "')"; 

$result = pg_query($query); 

printf ("These values are inserted into the database - %s %s %s", $entry->attributes()->title, $entry->attributes()->bathroom_number, $entry->attributes()->bedroom_number, $entry->attributes()->datasource_name, $entry->attributes()->guid, $entry->attributes()->img_url, $entry->attributes()->keywords, $entry->attributes()->lister_name, $entry->attributes()->listing_type, $entry->attributes()->price, $entry->attributes()->price_type, $entry->attributes()->property_type, $entry->attributes()->summary, $entry->attributes()->latitude, $entry->attributes()->longitude); 
} 

pg_close(); 

?> 

Yemi

+0

您沒有使用postGIS類型或函數。 – 2012-03-13 21:04:33

回答

0

如果POI沒有LAT-LON,該值它?空字符串? 假設在沒有緯度時返回空字符串。

$lon = "NULL"; 
if($entry->attributes()->longitude != "") 
$lon = "'".$entry->attributes()->longitude."'"; 
$lat = "NULL"; 
if($entry->attributes()->latitude != "") 
$lat = "'".$entry->attributes()->latitude."'"; 

所以查詢看起來是這樣的:

$query = "INSERT INTO nestoriaphp(title, bathroom, bedroom, datasource, guid, image, keywords, lister, listype, price, pricetype, property_type, summary, latitude, longitude) VALUES ('" . pg_escape_string($entry->attributes()->title) . "', '" . pg_escape_string($entry->attributes()->bathroom_number) . "', '" . pg_escape_string($entry->attributes()->bedroom_number) . "', '" . pg_escape_string($entry->attributes()->datasource_name) . "', '" . pg_escape_string($entry->attributes()->guid) ."', '" . pg_escape_string($entry->attributes()->img_url) . "', '" . pg_escape_string($entry->attributes()->keywords) . "', '" . pg_escape_string($entry->attributes()->lister_name) . "', '" . pg_escape_string($entry->attributes()->listing_type) . "', '" . pg_escape_string($entry->attributes()->price) . "', '" . pg_escape_string($entry->attributes()->price_type) . "', '" . pg_escape_string($entry->attributes()->property_type) ."', '" . pg_escape_string($entry->attributes()->summary) . "', " . $lat . ", " . $lon . ")"; 

注意,NULL值在SQL沒有'。

+0

您好,我無法重申一個網站返回的XML結構的值,我查詢它使用PHP後。我的代碼是:<?php $ url =(「http://api.wikimapia.org/?function=search&key=5A350EF4-7FCAE7D3-754A5A98-CABD8DEB-396C3791-1DB5A2FA-6F902A62-48136CB9&q=buildings&lat=51.5424&lon= -0.1734&計數= 20&頁= 1" ); $ xml = simplexml_load_file($ url); ($ xml-> place as $ entry){ echo $ entry-> attributes() - > id; echo $ entry-> attributes() - > name; echo $ entry-> attributes() - > url; echo $ entry-> attributes() - > lon; echo $ entry-> attributes() - > lat; } ?> – akinboj 2012-03-22 18:58:46