# Any mysql query to xml

**URL:** <https://forum.kirupa.com/t/any-mysql-query-to-xml/213992>\
**Category:** programming\
**Created:** [January 24, 2007, 8:20pm UTC](https://forum.kirupa.com/t/any-mysql-query-to-xml/213992 "2007-01-24T20:20:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfoston](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/tfoston/32/2516_2.png) [@tfoston](https://forum.kirupa.com/u/tfoston)\
**Post date:** [January 24, 2007, 8:20pm UTC](https://forum.kirupa.com/t/any-mysql-query-to-xml/213992/1 "2007-01-24T20:20:40Z")

</div>

Hello  
I’m a php newbie, and I’m guessing this might be out there, but I wrote a couple of functions to convert any mysql query to xml. Helps xml seems to be the way to pass loads of info from a php server to flash since i haven’t come across any major web hosting services to support amfphp.

There are 2 functions, 1 sorts the xml nodes by row, and the other lumps the info together by field. All you have to do is pass it your query result, and it will return the xml ($xml\_string)

Since I’m new if anyone has any comments please let me know. Again, I hope this helps someone

\<?php  
//Converts and Query to an returns an XML formated string, accepts a query object  
function QueryToXML\_Field($result){

//Define all variables  
$field\_array = Array(); //instance of a new array object to hold the field names in the recordset  
$field\_count = 0; //the amount of fields in the query  
$record\_count = mysql\_num\_rows($result); //The number of records returned from the query  
$xml\_string; //The resulting xml string

//Loop through the fields in the query results and add the field name to the array object  
while ($property = mysql\_fetch\_field($result)){  
$field\_array[$field\_count] = $property-\>name;  
$field\_count++;  
}

//Here I will start to build my xml document  
$xml\_string = “\<category\>”;

//Ok, now I have all the fields in an array. I want to cycle through the field array  
for($i=0;$i\<$field\_count;$i++){  
//I will sort the xml nodes by field name  
$xml\_string = $xml\_string."\<".$field\_array[$i]."\>";

//run a loop that will toss the contents of each row in the field in the xml document

```
           for($j=0;$j&lt;$record_count;$j++){
           $field_result = mysql_result($result,$j,$field_array[$i]);
           $xml_string = $xml_string."&lt;item&gt;".$field_result."&lt;/item&gt;";
           }//end loop
           
$xml_string = $xml_string."&lt;/".$field_array[$i]."&gt;";
}//end loop
$xml_string = $xml_string."&lt;/category&gt;";

//return the value of $sml_string as a result of the function
return $xml_string;

```

}

//function accepts a mysql query result object  
function QueryToXML\_Row($result){

//Define all variables  
$total\_records = mysql\_num\_rows($result); //number of rows returned from the query  
$fields\_array = Array(); //array that holds the name of the fields  
$count = 0; //How many fields in the query  
$xml\_string = ‘’; //The resulting xml string/documents

//Populate the fields\_array with the name of the fields  
while ($property = mysql\_fetch\_field($result)){  
$fields\_array[$count] = $property-\>name;  
$count++;  
}

//$count will also hold the amount of fields in the query/recordset

//Run the loop for every record in the recordset  
for($i=0;$i\<$total\_records;$i++){

$xml\_string = $xml\_string."\<row\>";  
//run loop here that goes through all the fields  
for($a=0;$a\<$count;$a++){  
$xml\_string = $xml\_string."\<".$fields\_array[$a]."\>";  
$xml\_string = $xml\_string.mysql\_result($result,$i,$fields\_array[$a]);  
$xml\_string = $xml\_string."\</".$fields\_array[$a]."\>";  
}//end loop  
$xml\_string = $xml\_string."\</row\>";  
}//end loop

//return the xml string as a result of the function  
return $xml\_string;  
}//end function

?\>
