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>
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.