本文介绍了如果加入两个或多个表,如何通过PHP将数据库转换为XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想使用php将我的wordpress数据库转换为xml代码,这个问题有很多代码,但它们都没有用于连接表格。 我尝试了什么: 我已经尝试过下面的PHP代码,但它不起作用,因为当我加入表格时我不能定义根元素,使树结构缺失 I want to convert my wordpress database to xml code using php, there are lot's of codes for this issue but none of them works for joining tables.What I have tried:I've tried the php code below but it doesn't work because when I join tables I can't define root element so the tree structure is missing <?php $config['mysql_host']="localhost"; $config['mysql_user']="user"; $config['mysql_pass']="pass"; $config['db_name']="name"; $config['table_name']="tbl1"; mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']); mysql_select_db($config['db_name']) or die("unable to select database"); $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"; $root_element = $config['table_name']."s"; //users $xml .= "<$root_element>"; //select all item intable $sql = "SELECT * FROM tbl1 join tbl2 on tbl1.id = tbl2.id"; $result = mysql_query($sql); if (!$result) { die('Invalid query: ' . mysql_error()); } if(mysql_num_rows($result)>0) { while($result_array = mysql_fetch_assoc($result)) { $xml .= "<".$config['table_name'].">"; //loop through each key,value pair in row foreach($result_array as $key => $value) { //$key holds the table column name $xml .= "<$key>"; //embed the SQL data in a CDATA element to avoid XML entity issues $xml .= "<![CDATA[$value"; //and close the element $xml .= "</$key>"; } $xml.="</".$config['table_name'].">"; } } //close the root element $xml .= "</$root_element>"; //send the xml header to the browser header ('Content-Type:text/xml; default_charset=utf-8;'); //output the XML data echo $xml; ?> 可能吗?我怎样才能做到这一点?任何建议都非常感谢。谢谢。Is it possibe at all? How can I do that? Any advice much appreciated. Thanks.推荐答案 config [' mysql_host'] = localhost; config['mysql_host']="localhost"; config [' mysql_user'] = user; config['mysql_user']="user"; config [' mysql_pass'] = pass; config['mysql_pass']="pass"; 这篇关于如果加入两个或多个表,如何通过PHP将数据库转换为XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 13:25