mysql_fetch_xml
| From: | Eliot Shepard | Date: | Thu, 23 Aug 2001 20:38:27 +0000 |
| Subject: | mysql_fetch_xml | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-64187@lists.php.net to get a copy of this message | ||
I have written a simple PHP function which returns all result rows
associated with a mysql query as an XML document. I needed this to
facilitate XSLT presentation of database records.
I was thinking about reimplementing the function in C and contributing
it to php_mysql.c. Would there be any interest in incorporating this
into the codebase?
Eliot
The PHP function:
<?php
function mysql_fetch_xml($result) {
// turn an mysql query result into an xml document.
$retval = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
$retval .= "<result>";
while ($row = mysql_fetch_assoc($result)) {
$retval .= "<row>";
while(list($key, $value) = each($row)) {
$retval .= "<$key>$value</$key>";
}
$retval .= "</row>";
}
$retval .= "</result>";
return $retval;
}
?>
Here's a usage example:
$rows = mysql_query($query, $dblink);
$xml = mysql_fetch_xml($rows);
$parser = xslt_create();
$xsl = join ('', file ('./present_rows.xsl'));
xslt_process($xsl, $xml, $result);
xslt_free($parser);
print $result;
--
Eliot Shepard
eliot.shepard@the451.com