2

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 Answer 1

2

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);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thanks. Was just looking at for and while loops on an array then came across your answer, Much appreciated!!

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.