下面提到的代码能够从数据库中获取数据,输出也是XML格式,但我想要的顺序不正确。
<?php
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "market";
$config['mysql_pass'] = "12345";
$config['db_name'] = "marketing";
$config['table_name'] = "data";
$root = customers;
//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $root;
$xml .= "<$root_element>";
//select all items in table
$sql = "SELECT * FROM ".$config['table_name'];
$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.=$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");
//output the XML data
echo $xml;
?>
当前输出:
<customers>
<data>
<customer>111</customer>
<openingbal>111</openingbal>
<pandl>11</pandl>
<currentbal>111</currentbal>
<time>2015-08-28 04:07:21</time>
</data>
<data>
<customer>111</customer>
<openingbal>111</openingbal>
<pandl>11</pandl>
<currentbal>111</currentbal>
<time>2015-08-28 04:07:21</time>
</data>
</customers>
但我希望下面的输出中id = "something">
但上面的代码不会产生相同的结果。
<Customers>
<Customer ID ="111">
<openingbal>111</openingbal>
<pandl>11</pandl>
<currentbal>111</currentbal>
<time>2015-08-14 03:14:30</time>
</Customer>
<Customer Id = "111">
<openingbal>111</openingbal>
<pandl>11</pandl>
<currentbal>111</currentbal>
<time>2015-08-14 03:14:30</time>
</Customer>
</Customers>
答案 0 :(得分:0)
这将解决问题。
<?php
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "market";
$config['mysql_pass'] = "12345";
$config['db_name'] = "marketing";
$config['table_name'] = "data";
$config['main_key'] = "customer";
$root = 'Customers';
//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $root;
$xml .= "<$root_element>";
//select all items in table
$sql = "SELECT * FROM ".$config['table_name'];
$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 .= "<".ucfirst($config['main_key']);
//loop through each key,value pair in row
foreach($result_array as $key => $value)
{
if ($key == $config['main_key']) {
$xml .= " ID = \"".$value."\">";
}else {
//$key holds the table column name
$xml .= "<$key>";
//embed the SQL data in a CDATA element to avoid XML entity issues
$xml.=$value;
//and close the element
$xml .= "</$key>";
}
}
$xml.="</".ucfirst($config['main_key']).">";
}
}
//close the root element
$xml .= "</$root_element>";
//send the xml header to the browser
header ("Content-Type:text/xml");
//output the XML data
echo $xml;
?>