Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert my wordpress database to xml code using php, there are lot's of codes for this issue but none of them works for joining tables.

What I have tried:

I've tried the php code below but it doesn't work because when I join tables I can't define root element so the tree structure is missing

PHP
<?php
      $config['mysql_host']="localhost";
      $config['mysql_user']="user";
      $config['mysql_pass']="pass";
      $config['db_name']="name";
      $config['table_name']="tbl1";

    mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
      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"; //users
      $xml         .= "<$root_element>";

      //select all item intable
      $sql = "SELECT * FROM tbl1 join tbl2 on tbl1.id = tbl2.id";

     $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 .= "<![CDATA[$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; default_charset=utf-8;');

    //output the XML data
    echo $xml;
    ?>


Is it possibe at all? How can I do that? Any advice much appreciated. Thanks.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900