0

I have some problem with converting mysql to xml using php due to my lack knowledge in PHP. I got this code from a website located here http://www.mightywebdeveloper.com/coding/mysql-to-xml-php/

<?php
header('Access-Control-Allow-Origin: *');

$oid= $_GET['oid'];

//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "thisisuser";
$config['mysql_pass'] = "thisispass";
$config['db_name']    = "mydb";
$config['table_name'] = "mail";

//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");

$xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."s"; //fruits
$xml         .= "<$root_element>";

//select all items in table
$sql = "SELECT * FROM mail WHERE oid='".$oid."' ORDER BY id ";

//SELECT * FROM ".$config['table_name'];

$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

if(mysql_num_rows($result)>0)
{
   while($result_array = mysql_fetch_assoc($result))
   {
      $xml .= "<".$config['table_name'].">";

      //loop through each key,value pair in row
      foreach($result_array as $key => $value)
      {
         //$key holds the table column name
         $xml .= "<$key>";

         //embed the SQL data in a CDATA element to avoid XML entity issues
         $xml .= "$value";

         //and close the element
         $xml .= "</$key>";
      }

      $xml.="</".$config['table_name'].">";
   }
}

//close the root element
$xml .= "</$root_element>";

//send the xml header to the browser
header ("Content-Type:text/xml");

//output the XML data
echo $xml;

?>

It work just fine after a few edit and the output goes like this.

<mails>
    <mail>
        <id>101221</id>
        <oid>1</oid>
        <from>Test User</from>
        <content>This is a test mail.</content>
    </mail>
        <mail>
        <id>101222</id>
        <oid>1</oid>
        <from>Test User</from>
        <content>This is a test mail.</content>
    </mail>
</mails>

My problem is Id like to display the array count next to the mail tag similar to something like

<mails>
    <mail  id="1"> <-fetched array number?
        <id>101221</id>
        <oid>1</oid>
        <from>Test User</from>
        <content>This is a test mail.</content>
    </mail>
        <mail  id="2">
        <id>101222</id>
        <oid>1</oid>
        <from>Test User</from>
        <content>This is a test mail.</content>
    </mail>
</mails>

Please help me.

4

1 Answer 1

1

Change this:

$xml .= "<".$config['table_name'].">";

To this:

$xml .= "<".$config['table_name']." id='".$result_array['id']."'>";
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thank you I got 1 last question instead of output the result array, id like it to be numbered. Like <mail id =1> <id>100</id> how to do that?
Something like this? <mail id ="1"> <id>10081818</id> </mail> <mail id ="2"> <id>1011109</id> </mail>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.