php - 使用php从xml中的mysql导出数据

标签 php mysql xml

我正在尝试使用 php 将数据从 mysql 数据库导出为特定的 xml 格式。

我是这样创建的。

如果我这样做,我会在 xml 中得到正确的 $string 输出。

<?php
$string = <<<_XML_
<videos>
<updated>2010-07-20T00:00:00Z</updated>
<video>
  <id>id</id>
  <title>title</title>
  <description>description</description>
  <tags>Comma,Separated,Keywords,Go,Here</tags>
  <paysite>Name Of site</paysite>
  <clip_url>http://www.domain.com/path/to/videos/</clip_url>
  <screen_url>http://www.domain.com/path/to/thumbnails/</screen_url>
  <clips>
  <clip>
  <duration>20</duration>
  <width>640</width>
  <height>480</height>
  <flv>marta_123.flv</flv>
  <screens>
  <screen>marta.jpg</screen>
  </screens>
  </clip>
  <clip>
  <duration>20</duration>
  <width>640</width>
  <height>480</height>
  <flv>jenna_123.flv</flv>
  <screens>
  <screen>jenna.jpg</screen>
  </screens>
  </clip>
  <clip>
  <duration>123</duration>
  <width>640</width>
  <height>480</height>
  <flv>kathy_123.flv</flv>
  <screens>
  <screen>kathy.jpg</screen>
  </screens>
  </clip>
  </clips>
 </video>
</videos>
_XML_;

$xml = new SimpleXMLElement($string);

Header('Content-type: text/xml');
echo $xml->asXML();
?>

但是如果我尝试从数据库中提取值并将其放入同一层次结构中,它不会以 xml 格式输出数据。像这样

    <?php
     $dbh=mysql_connect($localhost, $username, $password) or die ('I cannot connect to the database because: ' . mysql_error());
     $result = mysql_query("SELECT * FROM 12345_flv.flv WHERE enabled = '1' ORDER BY id DESC") or die('Could not connect: ' . mysql_error());  

    $string = '<<<_XML_<videos><updated>2010-07-20T00:00:00Z</updated><video>';
    while ($row = mysql_fetch_array($result)) { 
    $id=$row['id'];
    $title=$row['title'];
    $string .='<id>'.$id.'</id>';
    $string .='<title>'.$title.'</title>';
    }
    $string .='</video></videos>_XML_'; 
    $xml = new SimpleXMLElement($string);
    Header('Content-type: text/xml');
echo $xml->asXML();
?>

它的输出在页面源代码中显示 <<<XML

我做错了什么。 我想要的只是使用 php 将数据从 mysql 导出到 xml。

谢谢你的时间

最佳答案

试试这段代码:

<?php
$dbh=mysql_connect($localhost, $username, $password) or die ('I cannot connect to the database because: ' . mysql_error());
$result = mysql_query("SELECT * FROM 12345_flv.flv WHERE enabled = '1' ORDER BY id DESC") or die('Could not connect: ' . mysql_error());  

$string = '<videos><updated>2010-07-20T00:00:00Z</updated><video>';

while ($row = mysql_fetch_array($result)) { 
    $id=$row['id'];
    $title=$row['title'];
    $string .='<id>'.$id.'</id>';
    $string .='<title>'.$title.'</title>';
}
$string .='</video></videos>'; 
$xml = new SimpleXMLElement($string);
Header('Content-type: text/xml');
echo $xml->asXML();
?>

关于php - 使用php从xml中的mysql导出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14458603/

相关文章:

html - SVG 中的中心路径

php - 如何在 iOS 中使用 POST 请求向 SendGrid 发送照片电子邮件附件?

javascript - PHP session 变量不通过 AJAX 调用持久化

php - 为什么我们在 Windows 中需要 CURLOPT_SSL_VERIFYPEER

sql - MySQL记录存在性优化

mysql - 比较MySQL同一列的计数以获得百分比

javascript - 在 ASP.NET MVC 3 中使用 ajax 将 xml 字符串发送到 Controller

java - Java 中的 DTD 解析

php - 如何在有在线交易时向离线应用程序发送通知

php - 更新 pdo 中的两列