我试图使用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();
?>
但是,如果我尝试从db中提取值并放入相同的层次结构,它不会在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();
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。