<?php
    $query = $GLOBALS["HTTP_RAW_POST_DATA"];
	$mysql_host = $_GET["mysql_host"];
	$mysql_user = $_GET["mysql_user"];
	$mysql_password = $_GET["mysql_password"];
	$my_database = $_GET["my_database"];
	$limit_nom = $_GET["limit_nom"];
	$limit_row = $_GET["limit_row"];
    header('Content-Type: text/xml');
    header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header('Content-Disposition: inline; filename="res.xml"');
    header('Pragma: public');
	
	if ((isset($limit_nom) and isset($limit_row)))
	{
		$query = $query . " limit " . $limit_nom . ", " . $limit_row;	
	}

    $link = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die('<?xml version="1.0" encoding="utf-8"?><dump><error error="Ошибка подключения к MySQL серверу!!!"/></dump>');
    mysql_select_db($my_database) or die('<?xml version="1.0" encoding="utf-8"?><dump><error error="Не удалось выбрать базу данных!!!"/></dump>');
	$result = mysql_query("SET character_set_results = 'utf8'");
    $result = mysql_query($query) or die('<?xml version="1.0" encoding="utf-8"?><dump><error error="'.htmlspecialchars(mysql_error()).'"/></dump>');
	$res_xml = '<?xml version="1.0" encoding="utf-8"?><dump>';
	$kol_rez = true;
    while ($line = mysql_fetch_assoc($result)) 
	{
		if ($kol_rez)
		{
			$i = 0;
        	foreach ($line as $col_value) 
			{
				$res_xml = $res_xml . '<col field="' . mysql_field_name($result, $i) . '"/>';
				$i++;
        	}
			$kol_rez = false;
		}
        $res_xml = $res_xml . '<row';
		$i = 0;
        foreach ($line as $col_value) 
		{
			$res_xml = $res_xml . ' ' . mysql_field_name($result, $i) .'="' . htmlspecialchars($col_value) . '"';
			$i++;
        }
		$res_xml = $res_xml . '/>';
    }
    mysql_free_result($result);
    mysql_close($link);
	$res_xml = $res_xml . '</dump>';
	echo $res_xml;
?>
