加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:49:29 所属栏目:PHP教程 来源:网络整理
导读:我试图使用 PHP将数据从mysql数据库导出为特定的xml格式. 我是这样创造的. 如果我喜欢这个,我得到xml中$string的正确输出. ?php$string = _XML_videosupdated2010-07-20T00:00:00Z/updatedvideo idid/id titletitle/title descriptiondescription/descriptio
我试图使用 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,$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();
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读