DB2遍历用于创建XML文件的行


DB2 Iterating throught the rows for creating XML file

我遵循本教程将标记添加到谷歌地图上的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']);
}

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

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

  • DB2_连接
  • PHP手册-PDO_IBM
  • IBM手册-PDO_IBM

下面是一个在连接上使用带有try/catch的PDO Driver方法的工作示例。或者,您可以使用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);