Std DB query to XML results

This function takes a standard query and outputs the results to XML...

-----

function db_to_xml($query,$tag_l='<',$tag_r='>',$new_line="\n")
{
	global $db;

	echo "$tag_l?xml version=\"1.0\" ?$tag_r$new_line";

	if ( $results = $db->get_results($query) )
	{
		$record_num = 1;

		echo "{$tag_l}records count=\"".count($results)."\"{$tag_r}$new_line";

		foreach ( $results as $result )
		{

			echo "\t{$tag_l}record id=\"$record_num\"{$tag_r}$new_line";

			foreach ( $db->col_info as $col )
			{
				echo "\t\t$tag_l{$col->name}$tag_r{$result->{$col->name}}$tag_l/{$col->name}$tag_r$new_line";
			}

			echo "\t{$tag_l}/record{$tag_r}$new_line";

			$record_num++;
		}

		echo "{$tag_l}records{$tag_r}$new_line";

	}
	else
	{
		echo "{$tag_l}records count=\"0\"/{$tag_r}$new_line";
	}
}

Here is an example of usage and output...


// Usage
db_to_xml("select * from account_log");

// Output
<?xml version="1.0" ?>
<records count="3">
	<record id="1">
		<user_id>1</user_id>
		<action>1</action>
		<ip_address>127.0.0.1</ip_address>
		<hit_time>1149284088</hit_time>
	</record>
	<record id="2">
		<user_id>1</user_id>
		<action>2</action>
		<ip_address>127.0.0.1</ip_address>
		<hit_time>1149284102</hit_time>
	</record>
	<record id="3">
		<user_id>1</user_id>
		<action>5</action>
		<ip_address>127.0.0.1</ip_address>
		<hit_time>1149287196</hit_time>
	</record>
<records>

Hope it's useful.

Cheers,
JV