1
votes

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?

2
What happens if you execute a page containing this: <?php echo "hello world"; ?> ? - CoolStraw
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. - rik
rik your remark is likely to be the problem, I can't think of any other possibility. - CoolStraw
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'); - user643160
coud you var_dump($row); and see what output? - kn3l

2 Answers

0
votes

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>
0
votes

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