Hello.
I am trying to create XML file with some user informations. Since I am retrieving user information from 2 tables first I need to run some query to get all data.
Let's say this is my query:
$q = "SELECT * FROM table1,table2 WHERE table1.field1 = '$something' AND table1.field2 = table2.field1";
$result = mysql_query($q);
After that I construct XML tags in this way:
while ($row = mysql_fetch_array($result)){
$xml = "<user>\n\t";
$xml .= "<id>".$row['id']."</id>\n\t";
$xml .= "<username>".$row['username']."</username>\n\t";
$xml .= "<userlevel>".$row['userlevel']."</userlevel>\n\t";
$xml .= "<email>".$row['email']."</email>\n\t";
// etc.
$xml .= "</user>\n";
}
print ($xml);
In case that my query produces lots of details I would have to write manually each tag for my XML. But if I use same names for my XML tags as columns in my sql table i could probably do something like this:
while ($row = mysql_fetch_array($result)){
$xml = "<user>\n\t";
$xml .= "<".$column_name.">".$row['.$column_name.']."</".$column_name.">\n\t";
$xml .= "</user>\n";
}
print ($xml);
What I would like to ask is:
- how to retrieve column names from my query?
- how to add $column_name variable to my $row[] array? is this enough: ${"row[$column_name]"}
- since my query will probably have duplicate column names, how to avoid them?
Thanks for your help!