xml and php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
whitep8
Forum Newbie
Posts: 2
Joined: Tue Nov 17, 2009 4:00 am

xml and php

Post by whitep8 »

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.

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';
 
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: xml and php

Post by Grizzzzzzzzzz »

couple of simple examples.

Code: Select all

 
<?php
 
mysql_connect('localhost', 'username', 'password');
mysql_select_db("databasename");
 
$query="SELECT * FROM tablename";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
 
$completeString = "";
 
$completeString .= "<root>";
 
$i = 0;
 
while ($i < $rows)
{
    
$email = mysql_result($result,$i,"email");
$name = mysql_result($result,$i,"name");
$password = mysql_result($result,$i,"password");
$usertype = mysql_result($result,$i,"admin");
$last = mysql_result($result,$i,"last login");
 
$completeString .= "<Person name=\"".$name."\" email=\"".$email."\" password=\"".$password."\" usertype=\"".$usertype."\" lastLogin=\"".$last."\"/>";
 
$i++;
}
 
$completeString .= "</root>";
 
 
echo $completeString;
 
//simply save the string into a file
 
?>
 
 
or a different style of xml

Code: Select all

 
 
$completeString = "";
 
while ($i < $rows)
{
    
$email = mysql_result($result,$i,"email");
$name = mysql_result($result,$i,"name");
$password = mysql_result($result,$i,"password");
$usertype = mysql_result($result,$i,"admin");
$last = mysql_result($result,$i,"last login");
 
$completeString .=  "<Person><Name>".$name."</Name><Email>".$email."</Email><Password>".$password."</Password><Usertype>".$usertype."</Usertype><LastLogin>".$last."</LastLogin></Person>";
 
$i++;
}
 
echo $completeString;
 
//simply save the string into a file
 
 
whitep8
Forum Newbie
Posts: 2
Joined: Tue Nov 17, 2009 4:00 am

Re: xml and php

Post by whitep8 »

Hi,

Thanks for the examples.

The key think im trying to achieve is to create the xml from a PDO connection, which stores the query results in an array.
Post Reply