将多个DB表导出为SQL数据

时间:2011-06-27 09:40:46

标签: php xml

我希望将11个数据库表的数据导出为XML。我很容易设法导出一个表,没有问题。但我真的希望出口不止一个。

我确信这是一种方式,显然可以将数据输出为单独的表实体。任何帮助都非常感谢这一点,因为我发现它有点棘手。

我的代码如下

<?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 个答案:

答案 0 :(得分:2)

我改变了你的一些代码,假设你想要一些额外的东西:

  • 将str_replace()更改为htmlspecialchars()
  • 将xml开始移至开头。
  • 添加了新的根节点。

就是这样。如果要输出某个数据库中的所有表,则应使用“show tables;” -query找出db包含的表。

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

?>