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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用php从xml中的mysql导出数据脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用 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();
?>

脚本宝典总结

以上是脚本宝典为你收集整理的使用php从xml中的mysql导出数据全部内容,希望文章能够帮你解决使用php从xml中的mysql导出数据所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。