Kenwio Posted July 11, 2008 Share Posted July 11, 2008 Hi! I want my PHP-script to read the column names of a table and output the result to E4X-format. The TABLE NAME is - stations The FIELDS in the table 'stations' are: - station_id - staiton_name I've tried the following: <?php require_once("connection.php"); $query = "SELECT * FROM stations"; // This is the start of the XML response $Return = "<columnss>"; $result = mysql_query($query); $numfields = mysql_num_fields($result); while ($row = mysql_fetch_row($result)) { $Return .= "<columns><column>".mysql_field_name($row)->columns."</column></columns>"; } // This is the end of the XML-response $Return .= "</columnss>"; // This sends the XML-response print ($Return); ?> WAMP5 v1.7.4, MySQL v5.0.45 Please help!! //Christo Link to comment https://forums.phpfreaks.com/topic/114289-solved-please-help-mysql-column-names-to-e4x-xml/ Share on other sites More sharing options...
fenway Posted July 11, 2008 Share Posted July 11, 2008 What do you get now? Link to comment https://forums.phpfreaks.com/topic/114289-solved-please-help-mysql-column-names-to-e4x-xml/#findComment-587748 Share on other sites More sharing options...
Kenwio Posted July 11, 2008 Author Share Posted July 11, 2008 Hi! Yes, sorry for leaving that out! When I compile and run the Adobe Flex application in a browser, I get data but it seems that the data that's being retrieved are the actual rows/records from the table - not the column names. I'm showing the data in a DataGrid, and the following code works for viewing the records/rows: <?php $Query = "SELECT * from stations"; $Result = mysql_query( $Query ); $Return = "<stations>"; while ( $station = mysql_fetch_object( $Result ) ) { $Return .= "<station><station_id>".$station->station_id."</station_id><station_name>".$station->station_name."</station_name><station_location >".$station->station_location."</station_location></station>"; } $Return .= "</stations>"; mysql_free_result( $Result ); print ($Return); ?> The tag names are a bit different here, but that should not make a difference. Obviously, I'm using the mysql_field_name function in a wrong way somehow... Thanks for your time! Christo Link to comment https://forums.phpfreaks.com/topic/114289-solved-please-help-mysql-column-names-to-e4x-xml/#findComment-587752 Share on other sites More sharing options...
Kenwio Posted July 12, 2008 Author Share Posted July 12, 2008 This one is SOLVED. Instead of using <?php $Query = "SELECT * from stations"; $Result = mysql_query( $Query ); $Return = "<stations>"; while ( $station = mysql_fetch_object( $Result ) ) { $Return .= "<station><station_id>".$station->station_id."</station_id><station_name>".$station->station_name."</station_name><station_location >".$station->station_location."</station_location></station>"; } $Return .= "</stations>"; mysql_free_result( $Result ); print ($Return); ?> I used: <?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; ?> ...and it workds like a charm! Hope this will help someone in the future.. //Kenwio Link to comment https://forums.phpfreaks.com/topic/114289-solved-please-help-mysql-column-names-to-e4x-xml/#findComment-588327 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.