-1

我正在以 XML 格式从 MySQL 数据库输出数据,所以我制作了一个 php 文件,在页面上以 XML 格式显示数据库中的数据。

<?php
// Return all existing nid in array (Different types of form);
function nid(){
    $query = "SELECT w.nid 
                FROM webform w";
    $result = mysql_query($query);
    $table = array();
    while ($row = mysql_fetch_row($result)){
        $table[] = $row[0];
    }
    return $table;
}

// Return existing rows of corresponding nid
function sid($nid){
    $query = "SELECT ws.sid 
                FROM webform_submissions ws 
                WHERE ws.nid = $nid";
    $result = mysql_query($query);
    $table = array();
    while ($row = mysql_fetch_row($result)){
        $table[] = $row[0];
    }
    return $table;  
}
// Return corresponding components of nid;
function cid($nid){
    $query = "SELECT wc.form_key 
                FROM webform_component wc 
                WHERE wc.nid = $nid
                ORDER BY wc.cid ASC";
    $result = mysql_query($query);
    $table = array();
    while ($row = mysql_fetch_row($result)){
        $table[] = $row[0];
    }
    return $table;  
}

// Return values of fields corresponding to nid and sid
function data($nid, $sid){
    $query = "SELECT wsd.data 
                FROM webform_submitted_data wsd 
                WHERE wsd.nid = $nid AND wsd.sid = $sid
                ORDER BY wsd.sid ASC";
    $result = mysql_query($query);
    $table = array();
    while ($row = mysql_fetch_row($result)){
        $table[] = $row[0];
    }
    return $table;      
}
// Define Constants
DEFINE("DB_SERVER", "localhost");
DEFINE("DB_USER", "root");
DEFINE("DB_PASS", "");
DEFINE("DB_NAME", "drupal7");

//Establish Connection
mysql_connect(DB_SERVER, DB_USER, DB_PASS)
    or die("Unable to connect to MySQL");

//Select Database
mysql_select_db(DB_NAME)
    or die("Could not connect to the database");

$xml_output = "<?xml version=\"1.0\" ?>\n";
$xml_output .= "<schema>";

foreach (nid() as $nid){
    $xml_output .= "<form fid=\"$nid\">\n";
    foreach (sid($nid) as $sid){
        $xml_output .= "<row rid=\"$sid\">\n";
        for ($i = 0; $i < count(cid($nid)); $i++){
            $tag_array = cid($nid);
            $value_array = data($nid, $sid);
            $tag_row = $tag_array[$i];
            $value_row = $value_array[$i];
            $xml_output .= "<$tag_row>\n";
            $xml_output .= "$value_row\n";
            $xml_output .= "</$tag_row>\n";
        }
        $xml_output .= "</row>\n";
    }
    $xml_output .= "</form>\n";
}
$xml_output .= "</schema>";

header("Content-type: text/xml");
echo $xml_output;
mysql_close();

?>

但我无法找到访问 XML 数据或将其下载为 XML 文件的方法。

这是我运行它时输出的样子。 在此处输入图像描述

提前致谢

4

2 回答 2

1

不要使用字符串来构建您的 xml 数据。你应该使用类似 SimpleXMLElement 的东西

请参阅如何使用 PHP 动态生成 XML 文件?

于 2012-06-06T19:19:30.987 回答
0

代替:

$table[] = $row[0];

尝试使用 array_push 函数:

array_push($table,$row[0]);
于 2012-06-06T19:23:55.817 回答