Return to Snippet

Revision: 5372
at March 4, 2008 03:07 by Nils


Initial Code
//
// Use of function:
//
// $qstring = "select * from TABLE";
// $result = mysql_query($qstring);
// echo createXML_fromSQLResult($result);
//
// http://macdiggs.com/index.php/2006/07/05/integration-of-spry-and-phpmysql/
//


function createXML_fromSQLResult(&$result, $containerName="container", $elementName="element", $encoding="Shift_JIS")
    {
        //this functions creates XML output from the SQL result.
       
        $xml = <<<EOF
    <?xml version="1.0" encoding="{$encoding}" ?>
    <{$containerName}>
    EOF;
       
        while ($stuff = mysql_fetch_assoc($result)) {
            $xml .= "<{$elementName} id=\"{$stuff[id]}\">";
            foreach($stuff as $key=>$value) {
                $value = htmlspecialchars($value);
                $xml .= <<<EOF
                <{$key}>{$value}</{$key}>\n
    EOF;
            }
            $xml .= "</{$elementName}>\n";
        }

        $xml .= <<<EOF
    </{$containerName}>   
    EOF;

        return $xml;
   }

Initial URL
http://macdiggs.com/index.php/2006/07/05/integration-of-spry-and-phpmysql/

Initial Description
Stolen from the mentioned URL - this is used to make SPRY Framework work together with MYSQL.

Initial Title
FORM XML FROM MYSQL RESULTSET VIA PHP

Initial Tags
php, xml

Initial Language
PHP