Jump to content

Built in function to output mysql result set in XML schema?


JustinK101

Recommended Posts

Is there a built in php function which converts a mysql result object into strucutred and formatted XML? I know MSSQL has this option built into the language, but I dont think mysql does.

Example:

$sql = "SELECT first_name, last_name FROM logins ORDER BY last_name";
$result = mysql_query($sql);

Becomes

<resultSet>
  <logins>
    <first_name>Bob</first_name>
    <last_name>Apple</last_name>
  </logins>
  <logins>
    <first_name>James</first_name>
    <last_name>Bee</last_name>
  </logins>
</resultSet>
[code]
$sql = "SELECT first_name, last_name FROM logins ORDER BY last_name";
$result = mysql_query($sql);

if( !$result ) {
  die( "Query Failed...." );
}

while( $people = mysql_fetch_array($result) ) {
      print "<first_name>".$people['first_name']."</first_name>";
      print "<last_name>".$people['last_name']."</last_name>";
}
[/code]
HUmm looking for a standarized function, which doesnt know ahead of time how many rows/columns, and the column names. I think I may be able to write something. I will respond when I get something going.

Ehhhhhhhhhh, I wrote this real quick, not sure if it will work though, anybody want to test for me?

[code]
function mysql_to_xml($resultObj)
{
echo '<results>';
while($row = mysql_fetch_array($resultObj))
{
echo '<' . mysql_tablename($resultObj, 0) . '>';

for($i = 0; $i < mysql_num_rows($resultObj); $i++)
{

echo '<' . mysql_field_name($resultObj, $i) . '>' . $row[$i] . '</' . mysql_field_name($resultObj, $i) . '>';
}

echo '</' . mysql_tablename($resultObj, 0) . '>';
}

echo '</results>';
}
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.