使用Mysqli从SQL生成XML

问题描述 投票:0回答:3

我正在尝试从用户表中提取所有数据并以XML格式显示它。连接工作正常,因为我有一个登录和注册设置正常,但我似乎无法显示除白色屏幕以外的任何东西。

我已经找到了许多关于如何使用mysql而不是mysqli的不同教程。我错过了什么?

generatexml.php

 <?php
include 'connection.php';
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['users']; 
$xml .= "<$root_element>";

if ($result = $mysqli->query("SELECT * FROM users", MYSQLI_USE_RESULT)) {
                 while($row = $result->fetch_assoc())
                 { 
                 $xml .= "<".$config['users'].">";
                //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['users'].">";
    echo $xml;
   }

}

?>
php xml mysqli
3个回答
1
投票

我很难找到mysqli格式的解决方案,但我找不到解决方案。以下是我想到的解决方案。运行此演示并将其映射到您的要求,肯定会有所帮助。

<?php
//Create file name to save
$filename = "export_xml_".date("Y-m-d_H-i",time()).".xml";

$mysql = new Mysqli('server', 'user', 'pass', 'database');
if ($mysql->connect_errno) {
    throw new Exception(sprintf("Mysqli: (%d): %s", $mysql->connect_errno, $mysql->connect_error));
}

//Extract data to export to XML
$sqlQuery = 'SELECT * FROM t1';
if (!$result = $mysql->query($sqlQuery)) {
    throw new Exception(sprintf('Mysqli: (%d): %s', $mysql->errno, $mysql->error));
}

//Create new document 
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;

//add table in document 
$table = $dom->appendChild($dom->createElement('table'));

//add row in document 
foreach($result as $row) {
    $data = $dom->createElement('row');
    $table->appendChild($data);

    //add column in document 
    foreach($row as $name => $value) {

        $col = $dom->createElement('column', $value);
        $data->appendChild($col);
        $colattribute = $dom->createAttribute('name');
        // Value for the created attribute
        $colattribute->value = $name;
        $col->appendChild($colattribute);           
    }
}

/*
** insert more nodes
*/

$dom->formatOutput = true; // set the formatOutput attribute of domDocument to true 
// save XML as string or file 
$test1 = $dom->saveXML(); // put string in test1
$dom->save($filename); // save as file
$dom->save('xml/'.$filename);   
?>

1
投票

这是一个仅使用php的解决方案。你在哪里接近正确。这是代码的关键部分,我更改了“$ row as $ key => $ data”使用$ row而不是$ result_array,即。遍历行而不是result_array(这包含整个数据集)。希望这有助于某人。

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $value .="<record>\r\n";
                //loop through each key,value pair in row
                 foreach($row as $key => $data)
                  {
                   //$key holds the table column name
                   $vals = "\t" . "<". $key . ">" . "<![CDATA[" . $data . "]]>" . "</" . $key . ">" . "\r\n";
                   $value = $value . $vals;
                   //echo $value;
                  }
        $value .="</record>\r\n";
        $count++;
    }
} else {
    // echo "0 results";
}
$conn->close();

0
投票

一个可能的问题可能是这一行:

if ($result = $mysqli->query("SELECT * FROM users", MYSQLI_USE_RESULT)) {

尝试使用过程方法而不是面向对象的方法。我不知道在connection.php中是否定义了$ mysqli,但是你可能会将它混合起来。

if ($result = mysqli_query('SELECT * FROM users', MYSQLI_USE_RESULT)) {

这可以解决白屏错误。

我注意到另外两件事:

(1)一个微小的有效性问题:

$xml = '<?xml version="1.0" encoding="UTF-8"?>';

所以你不需要逃避每一个引号。

(2)一个严重的XML问题:在回显$ xml之前需要关闭根元素。

$xml .= "</$root_element>";
echo $xml;

通常,为了您的目的,使用PHP's XMLWriter extension更安全,如已经提出的那样。

© www.soinside.com 2019 - 2024. All rights reserved.