Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a new file in every iteration of a PHP program?
    primarykey
    data
    text
    <p>I am creating a file using PHP but I am unable to create a new file in every execution of the application. I am generating an XML file using a database. I want a new file in every execution of my code, how can I do that?</p> <p>This is my code:</p> <pre><code>&lt;?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 = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;"; $root_element = $config['table_name']."D1"; $xml .= "&lt;$root_element&gt;"; $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)&gt;0) { while($result_array = mysql_fetch_assoc($result)) { //$xml .= "&lt;".$config['table_name']."&gt;\n"; $xml .= "&lt;".$config['miosD1']."&gt;\n"; foreach($result_array as $key =&gt; $value) { if($value==NULL) { continue; } $xml .= "&lt;$key&gt;"; $xml .= "$value"; $xml .= "&lt;/$key&gt;\n"; } $xml.="&lt;/".$config['miosD1']."&gt;"; } } $xml .= "&lt;/$root_element&gt;"; header ("Content-Type:text/xml"); echo $xml; $file=fopen("xmlfilefirst.xml","a"); fwrite($file,$xml); fclose($file); } writeMsg(); $xml = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;"; $root_element = $config['table_name']."s"; $xml .= "&lt;$root_element&gt;"; $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)&gt;0) { while($result_array = mysql_fetch_assoc($result)) { $xml .= "&lt;".$config['table_name']."&gt;\n"; foreach($result_array as $key =&gt; $value) { if($value==NULL) { continue; } $xml .= "&lt;$key&gt;"; $xml .= "$value"; $xml .= "&lt;/$key&gt;\n"; } $xml.="&lt;/".$config['table_name']."&gt;"; } } $xml .= "&lt;/$root_element&gt;"; header ("Content-Type:text/xml"); echo $xml; $file=fopen("xmlfilefirst.xml","a"); fwrite($file,$xml); fclose($file); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload