簡體   English   中英

使用php從xml中的mysql導出數據

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

我試圖使用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();
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM