简体   繁体   English

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

[英]Exporting data from mysql in the xml using php

i am trying to export the data from mysql db into a specific xml format, using php. 我试图使用PHP将数据从mysql数据库导出为特定的xml格式。

i have created like this. 我是这样创造的。

if i do like this, i get proper output of a $string in 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();
?>

but if i try to pull values from db and put in the same hierarchy it doesnt output data in xml. 但是,如果我尝试从db中提取值并放入相同的层次结构,它不会在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();
?>

its output shows <<< XML in the page source . 它的输出显示页面源中的<<< XML

what i am doing wrong. 我做错了什么。 all i want is export the data from mysql into xml using php. 我想要的是使用php将数据从mysql导出到xml。

thanks for your time 谢谢你的时间

Try this code: 试试这段代码:

<?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();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM