Hi All,

Im wanting the following script to output to an XML file, but im getting an error on line 18 and i have hit a brick wall.

Help would be appreciated.

<?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->$obj());
fclose($fp);
?>

thanks for the info

Would this script, if i get it working as is, be ok to be called using simplexml using the dom parent child?

That is an option, also you can declare the header("Content-type: text/xml"); and construct like normal xml or echo out php iterations, works a charm!

ok brilliant. so the script itself creates the file, and i use simplexml to process it.

with regards to line 18 and 19, would it be possible for you to correct the syntax, as its creating a file, but theres nothing in it

hi all,

i see i had some major things missing in there!!

ive researched a bit and ive manaed to come up with the following....... vloser to my goal, but still a bit to go.....
Theres an error stating "syntax error: unexpected = on line 15"

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.php on line 15

here is the 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';  
?>

hi all,

i see i had some major things missing in there!!

ive researched a bit and ive manaed to come up with the following....... vloser to my goal, but still a bit to go.....
Theres an error stating "syntax error: unexpected = on line 15"

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.php on line 15

here is the 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';  
?>

or else you can make it more convinient with XMLWriter or manual examples, in case you just looking to write to the XML file.

hi network 18,

i dont understand your post. please could you explain?

hi network 18,

i dont understand your post. please could you explain?

just follow the links (in the blue color) from my post, there are some nice examples XMLWriter's code.Try to understand it, and start your code, after you stuck up somewhere post back.

"syntax error: unexpected = on line 15"

<?php

root=$doc->createElement('subscribers');//forgot $ sign

?>

end quote.

You forgot $ sign
Try this:

<?php

$root=$doc->createElement('subscribers');//inserted  $ sign

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.