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

Link to comment
Share on other sites

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
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.