Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6075615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:30:51+00:00 2026-05-23T10:30:51+00:00

I’m looking to export 11 Database tables’ data as XML. I’ve easily managed to

  • 0

I’m looking to export 11 Database tables’ data as XML. I’ve easily managed to export one table, without issue. But I am looking to export more than one really.

I’m sure theres a way and obviously output the data as seperate table entitys. Any help is much appreciated on this one, As I’m finding it a little tricky.

My code is as follows

<?php

error_reporting(E_ALL);

$host       = "localhost";
$user       = "root";
$pass       = "";
$database   = "db_etch";
$table = "keywords";

$SQL_query = "SELECT * FROM $table";

$DB_link = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $DB_link) or die ("Could not find or access the database.");
$result = mysql_query ($SQL_query, $DB_link) or die ("Data not found. Your SQL query didn't work... ");

// produce XML
header("Content-type: text/xml");
$XML = "<?xml version=\"1.0\"?>\n";

// root node
$XML .= "<result>\n";
// rows
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {    
  $XML .= "\t<$table>\n"; 
  $i = 0;
  // cells
  foreach ($row as $cell) {

    $cell = str_replace("&", "&amp;", $cell);
    $cell = str_replace("<", "&lt;", $cell);
    $cell = str_replace(">", "&gt;", $cell);
    $cell = str_replace("\"", "&quot;", $cell);
    $col_name = mysql_field_name($result,$i);
    $XML .= "\t\t<" . $col_name . ">" . $cell . "</" . $col_name . ">\n";
    $i++;
  }
  $XML .= "\t</$table>\n"; 
 }
$XML .= "</result>\n";

// output the whole XML string
echo $XML;

// Write $sql to file   
$File = "keywords.xml";
$fh = fopen($File, 'w') or die("can't open file");
$stringData = $XML;
fwrite($fh, $stringData);
fclose($fh);

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:30:52+00:00Added an answer on May 23, 2026 at 10:30 am

    I changed some of your code, assuming some little extra what you wanted:

    • Changed str_replace() to htmlspecialchars()
    • Moved xml start to the beginning.
    • Added a new root node.

    That’s about it. If you want to output all tables from a certain db, you should use “show tables;” -query to find out the tables that the db consist of.

    <?php
    
    error_reporting(E_ALL);
    
    $host       = "localhost";
    $user       = "root";
    $pass       = "";
    $database   = "db_etch";
    $table = "keywords";
    
    $tables_to_output_array = array('keywords', 'othertable1', 'othertable2');
    
    
    $DB_link = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
    
    mysql_select_db($database, $DB_link) or die ("Could not find or access the database.");
    
    // produce XML
    header("Content-type: text/xml");
    $XML = "<?xml version=\"1.0\"?>\n";
    // root node
    $XML .= "<tables>\n";
    
    while (list(, $table) = each($tables_to_output_array)) {
    
    
      $SQL_query = "SELECT * FROM $table";
    
      $result = mysql_query ($SQL_query, $DB_link) or die ("Data not found. Your SQL query didn't work... ");
    
      // tables
      $XML .= "\t<$table>\n";
      // rows
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $XML .= "\t\t<row>\n"; 
        $i = 0;
        // cells
        foreach ($row as $cell) {
          $col_name = mysql_field_name($result,$i);
          $XML .= "\t\t\t<" . $col_name . ">" . htmlspecialchars($cell) . "</" . $col_name . ">\n";
          $i++;
        }
        $XML .= "\t\t</row>\n"; 
       }
      $XML .= "\t</$table>\n";
    
    }
    
    $XML .= "</tables>\n";
    
    // output the whole XML string
    echo $XML;
    
    // Write $sql to file   
    $File = "keywords.xml";
    $fh = fopen($File, 'w') or die("can't open file");
    $stringData = $XML;
    fwrite($fh, $stringData);
    fclose($fh);
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but
I have a reasonable size flat file database of text documents mostly saved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.