1

im trying to create a xml file using php.everytime i run the code the page displayes the code from a certain point as text on the screen.the code i hav is as follows:

<?php
    if(!$dbconnet = mysql_connect('I took out the details')){
        echo "connection failed to the host.";
        exit;
    }
    if (!mysql_select_db('siamsati_db')){
        echo "Cannot connect to the database.";
        exit;
    }
    $table_id = 'events';
    $query = "SELECT * FROM $table_id";
    $dbresult = mysql_query($query, $dbconnect);

    $doc = new DomDocument('1.0');

    $root = $doc->createElement('root');
    $root = $doc->appendChild($root);

    while($row = mysql_fetch_assoc($dbresult)){
        $ooc = $doc->createElement($table_id);
        $occ = $root->appendChild($occ);

        foreach ( $row as $fieldname => $fieldvalue){

            $child = $doc->createElement($fieldname);
            $child = $occ->appendchild($child);

            $value = $doc->createTextNode($fieldvalue);
            $value = $child->appendChild($value);
        }
    }

    $xml_string = $doc->saveXML();
    echo $xml_string;

?>

and the page when displayed shows:

createElement('root'); $root = $doc->appendChild($root); while($row = mysql_fetch_assoc($dbresult)){ $ooc = $doc->createElement($table_id); $occ = $root->appendChild($occ); foreach ( $row as $fieldname => $fieldvalue){ $child = $doc->createElement($fieldname); $child = $occ->appendchild($child); $value = $doc->createTextNode($fieldvalue); $value = $child->appendChild($value); } } $xml_string = $doc->saveXML(); echo $xml_string; ?>

is there something ive missed.ive checked all the quotes thinking it was that at first but they all seem to be right.any suggestions on what im doing wrong are much appreciated?

6
  • What happens if you execute a page containing this: <?php echo "hello world"; ?> ? Commented Mar 15, 2011 at 14:39
  • Is there a ? before the > in the line that reads $root = $doc->createElement('root');? That would make the PHP interpreter switch to HTML mode and dump everything that follows giving the output you posted. At least I cannot imagine you compiled PHP with -> as the closing tag. That'd be totally sick. Commented Mar 15, 2011 at 14:39
  • rik your remark is likely to be the problem, I can't think of any other possibility. Commented Mar 15, 2011 at 14:47
  • The code above is exactly the same code as i hav except i left out the closing tag at the end in the code above.theres no ? before the > in the line $root = $doc->createElement('root'); Commented Mar 15, 2011 at 15:08
  • coud you var_dump($row); and see what output? Commented Mar 15, 2011 at 15:50

2 Answers 2

0

Set the content type to be XML, so that the browser will recognise it as XML.

header( "content-type: application/xml; charset=ISO-8859-15" );

In your code Change it to:

// Set the content type to be XML, so that the browser will   recognise it as XML.
header( "content-type: application/xml; charset=ISO-8859-15" );

// "Create" the document.
$doc = new DOMDocument( "1.0", "ISO-8859-15" );

+++I think you can do something like this

<root>
<?
   foreach ( $row as $fieldname => $fieldvalue){
    ?>
    <events>
        <fieldname><?=fieldname; ?></fieldname>
        <fieldvalue><?=$fieldvalue; ?></fieldvalue>
    </events>
    }
?>
</root>
Sign up to request clarification or add additional context in comments.

Comments

0

In the code you've posted here the initial <?php tag is missing...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.