使用php在xml中从mysql导出数据 [英] Exporting data from mysql in the xml using php
本文介绍了使用php在xml中从mysql导出数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用php将mysql数据库中的数据导出为特定的xml格式.
i am trying to export the data from mysql db into a specific xml format, using php.
我是这样创建的.
如果我这样做,我会在xml中正确获取$ string的输出.
if i do like this, i get proper output of a $string in xml.
<?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();
?>
但是,如果我尝试从db中提取值并放入相同的层次结构中,则它不会以xml输出数据.像这样
but if i try to pull values from db and put in the same hierarchy it doesnt output data in xml. like this
<?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 .
its output shows <<<XML in the page source .
我在做什么错. 我想要的就是使用php将mysql中的数据导出到xml中.
what i am doing wrong. all i want is export the data from mysql into xml using php.
感谢您的时间
推荐答案
尝试以下代码:
<?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在xml中从mysql导出数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文