在我的Api中,我必须在地图中显示从数据库中获取经度和纬度的几个地方,我在php中工作,如何做到这一点?
答案 0 :(得分:4)
Google tutorial很棒。但是因为它使用了已弃用的mysql_扩展,我将其修改为使用PDO。请参见地图HERE
<?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;
?>
您可以查看生成的HERE
<强> phpsqlajax_dbinfo.php 强>
<?
$username="username";
$password="password";
$database="username-databaseName";
?>
答案 1 :(得分:2)