Kenwio Posted July 12, 2008 Share Posted July 12, 2008 Hi! I want to output the field names of a MySQL database table to an XML-structure through PHP. I've managed to echo the field names, but all the field names are echoed as ONE OBJECT. Here's the PHP code: <?php require_once("connection.php"); $res = mysql_query('SELECT * FROM stations'); // This begins the XML-response $return = "<stations>"; $return .= "<station>"; $numberfields = mysql_num_fields($res); for ($i=0; $i<$numberfields ; $i++ ) { $var = mysql_field_name($res, $i); $return .= "<station_id>".$var->station_id."</station_id>"; } $return .= "</station>"; // This ends the XML-response $return .= "</stations>"; echo $return; ?> ...and here's the result in a browser: station_idstation_namestation_typestation_frequencystation_location... etc Now, I now that mysql_fetch_object works on rows in a database, but does it work with actual field names? Is it possible to use mysql_fetch_object on field/column names when using mysql_field_name? TABLE NAME: - stations COLUMN NAMES OF stations: - station_id - station_name - station_type... etc WAMP5 v1.7.4, MySQL v5.0.45 Please if you have any ideas, Post-it! //Kenwio Quote Link to comment Share on other sites More sharing options...
Kenwio Posted July 12, 2008 Author Share Posted July 12, 2008 This is solved. Instead of: <?php $res = mysql_query('SELECT * FROM stations'); $return = "<stations>"; $return .= "<station>"; $numberfields = mysql_num_fields($res); for ($i=0; $i<$numberfields ; $i++ ) { $var = mysql_field_name($res, $i); $return .= "<station_id>".$var."</station_id>"; } $return .= "</station>"; $return .= "</stations>"; echo $return; ?> I use: <?php $res = mysql_query('SELECT * FROM stations'); $return = "<stations>"; $numberfields = mysql_num_fields($res); for ($i=0; $i<$numberfields ; $i++ ) { $var = mysql_field_name($res, $i); $return .= "<station><station_id>".$var."</station_id></station>"; } $return .= "</stations>"; echo $return; ?> Now each field columns name is treated as ONE OBJECT. Hope this will help someone in the future... //Kenwio Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.