FORM XML FROM MYSQL RESULTSET VIA PHP


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. //
  2. // Use of function:
  3. //
  4. // $qstring = "select * from TABLE";
  5. // $result = mysql_query($qstring);
  6. // echo createXML_fromSQLResult($result);
  7. //
  8. // http://macdiggs.com/index.php/2006/07/05/integration-of-spry-and-phpmysql/
  9. //
  10.  
  11.  
  12. function createXML_fromSQLResult(&$result, $containerName="container", $elementName="element", $encoding="Shift_JIS")
  13. {
  14. //this functions creates XML output from the SQL result.
  15.  
  16. $xml = <<<EOF
  17. <?xml version="1.0" encoding="{$encoding}" ?>
  18. <{$containerName}>
  19. EOF;
  20.  
  21. while ($stuff = mysql_fetch_assoc($result)) {
  22. $xml .= "<{$elementName} id=\"{$stuff[id]}\">";
  23. foreach($stuff as $key=>$value) {
  24. $value = htmlspecialchars($value);
  25. $xml .= <<<EOF
  26. <{$key}>{$value}</{$key}>\n
  27. EOF;
  28. }
  29. $xml .= "</{$elementName}>\n";
  30. }
  31.  
  32. $xml .= <<<EOF
  33. </{$containerName}>
  34. EOF;
  35.  
  36. return $xml;
  37. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.