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.

learningXD
  • 11
  • 1
  • 3
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 09 '15 at 19:36
  • you'd have to detect which "note" you're on in the query results, and generate the html appropriately. e.g. `$row = fetch(); if ($row is id) { output mail id=1 } else { build regular node }` – Marc B Jun 09 '15 at 19:40
  • You dont escape xml. You are on highway to hell. – venca Jun 09 '15 at 19:41
  • create xml via http://php.net/manual/en/book.simplexml.php or http://php.net/manual/en/book.dom.php. Thats safe and correct way. – venca Jun 09 '15 at 19:51

1 Answers1

1

Change this:

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

To this:

$xml .= "<".$config['table_name']." id='".$result_array['id']."'>";
winmutt
  • 405
  • 2
  • 7