我试图使用PHP将数据从mysql数据库导出为特定的xml格式.
我是这样创造的.
如果我喜欢这个,我得到xml中$string的正确输出.
$string = <<<_xml_>
2010-07-20T00:00:00Z
id
titledescription
Comma,Separated,Keywords,Go,Here
Name Of site
http://www.domain.com/path/to/videos/
http://www.domain.com/path/to/thumbnails/
20
640
480
marta_123.flv
marta.jpg
20
640
480
jenna_123.flv
jenna.jpg
123
640
480
kathy_123.flv
kathy.jpg
_XML_;
$xml = new SimpleXMLElement($string);
Header('Content-type: text/xml');
echo $xml->asXML();
?>
但是,如果我尝试从db中提取值并放入相同的层次结构,它不会在xml中输出数据.像这样
$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_>2010-07-20T00:00:00Z';
while ($row = mysql_fetch_array($result)) {
$id=$row['id'];
$title=$row['title'];
$string .=''.$id.'';
$string .='
'.$title.'';}
$string .='_XML_';
$xml = new SimpleXMLElement($string);
Header('Content-type: text/xml');
echo $xml->asXML();
?>
其输出在页面源中显示<<<< XML.
我做错了什么.
我想要的是使用php将数据从mysql导出到xml.
谢谢你的时间
解决方法:
试试这段代码:
$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 = '2010-07-20T00:00:00Z';
while ($row = mysql_fetch_array($result)) {
$id=$row['id'];
$title=$row['title'];
$string .=''.$id.'';
$string .='
'.$title.'';}
$string .='';
$xml = new SimpleXMLElement($string);
Header('Content-type: text/xml');
echo $xml->asXML();
?>
标签:php,mysql,xml
来源: https://codeday.me/bug/20190629/1329312.html