我正在遵循本教程,将标记从我的DB2数据库添加到谷歌地图上的某些地址:https://developers.google.com/maps/articles/phpsqlajax_v3 我正处于尝试从DB2数据库创建XML文档的部分,我感到有些困惑。这是谷歌编写的代码,用于迭代行并从中创建XML文档:
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// 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']);
}
使用PHP在DB2中执行此操作的等效方法是什么?
答案 0 :(得分:0)
您需要使用相应的PHP DB2 API,这可能涉及在php ini文件中启用扩展并可能安装DB2 ODBC驱动程序:
下面是一个使用PDO Driver方法和连接上的try / catch的工作示例。或者,您可以使用DSN(参见上面的手册):
# Opening db connection
try {
$conn = db2_connect($database, $user, $password);
$sql = "SELECT * FROM tablename";
$stmt = db2_prepare($conn, $sql);
$result = db2_execute($stmt);
}
catch(PDOException $e) {
echo $e->getMessage();
exit;
}
while($row = db2_fetch_assoc($stmt)) {
// 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']);
}
# Closing db connection
db2_close($conn);