simple xml and php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whitep8
    New Member
    • Oct 2009
    • 65

    #1

    simple xml and php

    Hi all,

    im a little bit puzzled with this one.

    Im after creating a simplexml encoded file using php, which calls from a mysql database.

    i have the following, but i dont see what is wrong with the syntax.

    Code:
    <?php
    require_once("connect.inc.php");
       $db = getConnectionMySql();
       $stmt = $db->query("SELECT email, name, lastlogin FROM subscriber");
       while ( $obj = $stmt->fetchObject()){
    	   echo $obj->email . ":" . $obj->name . ":" . $obj->lastlogin .  "<br />";
       	//   print " <row>\n";
          // print "  <email>$email</email>\n";
           //print "  <name>$name</name>\n";
    	   //print " <lastlogin>$lastlogin</lastlogin>\n";
           //print " </row>\n";
       }
       
       $fp = fopen('subscribers.xml','w');
    	if(!$fp) {
        die('Error cannot create XML file');
    }
    fwrite($fp,$stmt());
    //fwrite( $fp, implode(”,$obj));
    fclose($fp);
    echo "XML has been written.  <a href=\"subscribers.xml\">View the XML.</a>";
    
    ?>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I could probably tell you something, if I knew what you were doing. there are no error messages told, neither is anything related to the objects you use. All I can say right now is that there is no XML root element.

    Comment

    • whitep8
      New Member
      • Oct 2009
      • 65

      #3
      Hi,

      Ive had another look and I can see ive missed some major stuff.

      I know i should have created a root and some parent child data etc.

      Ive created the following, with the following error

      Parse error: syntax error, unexpected '=' in /Applications/MAMP/htdocs/CG0119/subscribelist.p hp on line 15

      here is the code

      Code:
      <?php
      include_once("connect.inc.php");
        $db = getConnectionMySql();
         $stmt = $db->query("SELECT email, name, lastlogin FROM subscriber");
         while ( $obj = $stmt->fetchObject()){
      	   echo $obj->email . ":" . $obj->name . ":" . $obj->lastlogin .  "<br />";
         	//   print " <row>\n";
            // print "  <email>$email</email>\n";
             //print "  <name>$name</name>\n";
      	   //print " <lastlogin>$lastlogin</lastlogin>\n";
             //print " </row>\n";
         }
         
      $doc = new DomDocument('1.0'); 
      root=$doc->createElement('subscribers');
      $root = $doc->appendChild($root);  
      while($row = mysql_fetch_assoc($result))
      {
          // add node for each record
          $email= $doc->createElement('email');
          $email = $root->appendChild($email);
      }  
      foreach($row as $fieldname => $fieldvalue)
      {
          $child = $doc->createElement($fieldname);
          $child = $email->appendChild($child);
          //add data to the new element
          $value = $doc->createTextNode($fieldvalue);
          $value = $child->appendChild($value);
      }  
      echo 'Wrote: ' . $doc->save("subscribers.xml") . '  bytes';  
      ?>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by whitep8
        Parse error: syntax error, unexpected '=' in /Applications/MAMP/htdocs/CG0119/subscribelist.p hp on line 15
        $ missing.

        Code:
        $email = $root->appendChild($email);
        // does the same as
        $root->appendChild($email);
        but you don’t define the text content of that node…

        Comment

        Working...