在我的 Api 中,我必须在地图中显示几个地方,其经度和纬度是从数据库中获取的,我正在使用 php,这将如何完成?
问问题
1456 次
2 回答
4
谷 歌教程非常好。但由于它使用了已弃用的 mysql_ 扩展,我已将其修改为使用PDO。在这里查看地图
<?php
require("phpsqlajax_dbinfo.php");
// 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
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$stmt = $dbh->query('SELECT * FROM markers WHERE 1');
// setting the fetch mode
$stmt->setFetchMode(PDO::FETCH_ASSOC);
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while($row = $stmt->fetch()) {
// ADD TO XML DOCUMENT NODE
$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("type", $row['type']);
}
echo $dom->saveXML();
}
catch(PDOException $e) {
echo "Error .". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", phpsqlajax_genxml.php , ". $e->getMessage()."\r\n", FILE_APPEND);
}
//Close the connection
$dbh = null;
?>
您可以在此处查看生成的 XML
phpsqlajax_dbinfo.php
<?
$username="username";
$password="password";
$database="username-databaseName";
?>
于 2012-12-09T14:48:11.373 回答
2
于 2012-12-09T12:53:34.483 回答