DB2 遍历行以创建 XML 文件

DB2 Iterating throught the rows for creating XML file

我正在按照本教程将标记添加到我的 DB2 数据库中的某些地址到 google 地图上:https://developers.google.com/maps/articles/phpsqlajax_v3 我正在尝试从我的 DB2 数据库创建一个 XML 文档,但有点困惑。这是 google 编写的用于遍历行并从中创建 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']);
}

在 DB2 中使用 PHP 这样做的等效方法是什么?

您将需要使用相应的 PHP DB2 API,这可能涉及在 php ini 文件中启用扩展并可能安装 DB2 ODBC 驱动程序:

下面是一个使用 PDO 驱动程序方法并在连接上使用 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);