Jump to content

[SOLVED] PLEASE HELP: MySQL column names to E4X XML


Kenwio

Recommended Posts

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

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

 

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!  :D

 

Hope this will help someone in the future..

//Kenwio

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.