I don't remember why I built this. It's a good start anyway. The important parts are getColumns, and the while loop just after the xml content declaration. It could be built as a class easy enough.
<html>
<body>
<?php
if ($submit){
$db_host = "";
$db_username = "";
$db_password = "";
$db_name = "";
$dbhandle = @mysql_connect($db_host, $db_username, $db_password);
$connectError = false;
if (!@mysql_select_db($db_name, $dbhandle)){
echo mysql_error();
$connectError = true;
}
if ($user_db != "" && $user_table != "" && $user_db != "mysql" && !$connectError){
$status = @mysql_select_db($user_db);
$table = "$user_table";
$cols = getColumns($table);
$res = @mysql_query("SELECT * FROM $table ");
$final = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<SQLtoPHP>\n";
while ($item = @mysql_fetch_array($res)) {
$final .= " <$user_table>\n";
for ($i = 0; $i < count($cols); $i++){
$value = quotemeta ($item["{$cols[$i]}"]);
$value = htmlspecialchars($item["{$cols[$i]}"]);
$final .="\t\t<{$cols[$i]}>$value</{$cols[$i]}>\n";
}
$final .= "\t</$user_table>\n";
}
$final .= " </SQLtoPHP>\n";
$fp = fopen("./work/$user_table.xml", "w");
fputs($fp, $final);
fclose ($fp);
getScript($user_table);
}
else {
echo "<br>Beat it ";
}
@mysql_close($dbhandle);
}
else{ //don't show page.
}
?>
<form name="test" method="get" action="sqltoxml_gen.php">
DB<input type="text" name="user_db" value="<? echo $user_db; ?>"><br>
Table<input type="text" name="user_table" value="<? echo $user_table; ?>"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<?
function getColumns($table){
GLOBAL $db_host, $db_username, $db_password, $db_name;
$dbhandle = mysql_connect($db_host, $db_username, $db_password);
$status = mysql_select_db($db_name, $dbhandle);
$result = mysql_query("select * from $table LIMIT 0,1");
for ($i = 0; $i < mysql_num_fields($result); $i++) {
$cols[$i] = mysql_field_name($result, $i);
}
return $cols;
}
function getScript($user_table){
echo '<SCRIPT LANGUAGE="JavaScript">
function setLocation() {
timerWindow = window.open("./work/'.$user_table.'.xml", "xml", "width=640,height=500,resizable=yes");
timerWindow.focus();
}
setLocation();
</SCRIPT>';
}
?>
the form will request the database, and table you want to dump. It will need write acess to the work directory.
ShriekForth