我正在使用 PHP 创建一个文件,但我无法在应用程序的每次执行中创建一个新文件。我正在使用数据库生成 XML 文件。我希望在每次执行代码时都有一个新文件,我该怎么做?
这是我的代码:
<?php
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "root";
$config['db_name'] = "dcu";
$config['table_name'] = "readingsThreePhase";
//$config['table_name1'] = "miosD1";
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
@mysql_select_db($config['db_name']) or die( "Unable to select database");
function writeMsg()
{
//echo "Hello world!";
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."D1";
$xml .= "<$root_element>";
$sql = "SELECT * FROM dcu.readingsThreePhase where meterId=3 order by recordTime DESC limit 1";
$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'].">\n";
$xml .= "<".$config['miosD1'].">\n";
foreach($result_array as $key => $value)
{
if($value==NULL)
{
continue;
}
$xml .= "<$key>";
$xml .= "$value";
$xml .= "</$key>\n";
}
$xml.="</".$config['miosD1'].">";
}
}
$xml .= "</$root_element>";
header ("Content-Type:text/xml");
echo $xml;
$file=fopen("xmlfilefirst.xml","a");
fwrite($file,$xml);
fclose($file);
}
writeMsg();
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."s";
$xml .= "<$root_element>";
$sql = "SELECT * FROM dcu.readingsThreePhase where meterId=3 order by recordTime DESC limit 1";
$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'].">\n";
foreach($result_array as $key => $value)
{
if($value==NULL)
{
continue;
}
$xml .= "<$key>";
$xml .= "$value";
$xml .= "</$key>\n";
}
$xml.="</".$config['table_name'].">";
}
}
$xml .= "</$root_element>";
header ("Content-Type:text/xml");
echo $xml;
$file=fopen("xmlfilefirst.xml","a");
fwrite($file,$xml);
fclose($file);
?>