xml and php
Posted: Tue Nov 17, 2009 4:03 am
Hi All,
I have a database with the following fields: email, name password, admin, last login
Im trying to get php to create an xml document from the mysql table. Im kinda getting there but with a few teething problems.
Im using pdo to connect and then storing the data in an array. Hoping somebody could help be fix my syntax please.
I have a database with the following fields: email, name password, admin, last login
Im trying to get php to create an xml document from the mysql table. Im kinda getting there but with a few teething problems.
Im using pdo to connect and then storing the data in an array. Hoping somebody could help be fix my syntax please.
Code: Select all
<?php
require_once("connect.inc.php");
$db = getConnectionMySql();
$stmt = $db->query("SELECT email, name, lastlogin FROM subscriber");
$doc = new DomDocument('1.0','UTF-8');
$root = $doc->createElement('subscribers');
$root = $doc->appendChild($root);
// process all rows
while ( $obj = $stmt->fetchObject()){
// add node for each record
$email = $doc->createElement('email');
$email = $root->appendChild($email);
foreach ($row as $fieldName => $fieldValue)
{
// add a child node for each field
$child = $doc->createElement($fieldName);
$child = $email->appendChild($child);//apend to car
$value = $doc->createTextNode($fieldValue);//set text to add
$value = $child->appendChild($value);//add it to new node (child of car)
}
}
echo 'Wrote: ' . $doc->save("subscriber.xml") . ' bytes';