Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php在xml中从mysql导出数据_Php_Mysql_Xml - Fatal编程技术网

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

使用php在xml中从mysql导出数据,php,mysql,xml,Php,Mysql,Xml,我正在尝试使用php将数据从mysql数据库导出为特定的xml格式 我是这样创造的 如果我这样做的话,我会得到一个xml格式的$string的正确输出 <?php $string = <<<_XML_ <videos> <updated>2010-07-20T00:00:00Z</updated> <video> <id>id</id> <title>title</title

我正在尝试使用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();
?>
asXML();
?>
但是,如果我尝试从数据库中提取值并将其放在同一层次结构中,它不会以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();
?>
asXML();
?>
其输出显示请尝试以下代码:

<?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();
?>
asXML();
?>
看看php手册中的“SimpleXML”