PHP MySQL XML
###質問
1.下記ソースコード中、「あ」「い」と同じコードが記載されているので「あ」は不要ではないでしょうか。
2.下記ソースコード中、「う」の目的は何でしょうか。
下記phpをブラウザからコールしたとき、saveXML()の内容をブラウザに表示するたものものでしょうか。
3.下記ソースコード中、「え」のsaveXML()の内容をブラウザに表示するにはどうすればいいのでしょうか。
(該当ページにある下記説明とおり実施したつもりですが、ブラウザに
<?xml version="1.0"?> <markers/>
と表示され、明細は表示されませんでした。)
Checking That the XML Output Works
Call this PHP script from the browser to make sure it's producing valid XML. If you suspect there's a problem with connecting to your database, you may find it easier to debug if you remove the line in the file that sets the header to the text/xml content type, as that usually causes your browser to try to parse XML and may make it difficult to see your debugging messages.
以上、よろしくお願い致します。
###該当のソースコード
目的は、PHPでmySQLからデータ取得してXMLで書きだすことです。
【書き出した結果例】
<?xml version="1.0"?> <markers> <marker name="Round Table Pizza: Mountain View" address="570 N Shoreline Blvd, Mountain View, CA" lat="37.402653" lng="-122.079353" distance="0.38091455044131"/> <marker name="Kapp's Pizza Bar & Grill" address="191 Castro St, MountainView, CA" lat="37.393887" lng="-122.078918" distance="0.5596115438175"/> ... </markers>
上記書き出しのためのソースコードは下記のとおりです。
(https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=ja#createmap の説明文のソースコードです。)
<?php require("phpsqlsearch_dbinfo.php"); // Get parameters from URL $center_lat = $_GET["lat"]; $center_lng = $_GET["lng"]; $radius = $_GET["radius"]; // Start XML file, create parent node $dom = new DOMDocument("1.0"); $node = $dom->createElement("markers"); $parnode = $dom->appendChild($node); // Opens a connection to a mySQL server $connection=mysql_connect (localhost, $username, $password); if (!$connection) { die("Not connected : " . mysql_error()); } // Set the active mySQL database $db_selected = mysql_select_db($database, $connection); if (!$db_selected) { die ("Can\'t use db : " . mysql_error()); } // Search the rows in the markers table $query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", mysql_real_escape_string($center_lat), mysql_real_escape_string($center_lng), mysql_real_escape_string($center_lat), mysql_real_escape_string($radius)); $result = mysql_query($query); //・・・あ $result = mysql_query($query); //・・・い if (!$result) { die("Invalid query: " . mysql_error()); } header("Content-type: text/xml"); //・・・う // Iterate through the rows, adding XML nodes for each while ($row = @mysql_fetch_assoc($result)){ $node = $dom->createElement("marker"); $newnode = $parnode->appendChild($node); $newnode->setAttribute("name", $row['name']); $newnode->setAttribute("address", $row['address']); $newnode->setAttribute("lat", $row['lat']); $newnode->setAttribute("lng", $row['lng']); $newnode->setAttribute("distance", $row['distance']); } echo $dom->saveXML(); //・・・え ?>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/11 13:30
退会済みユーザー
2016/10/12 01:14
2016/10/12 10:47