XML repeats record when generated in PHP

0

I have a PHP script that generates XML file, however something very strange is that it repeats several records from my table of real estate and the worse still is that there is no repeated real estate. Repeat the property ID with only one property for each ID.

Here is my code that generates XML:

$arquivo = "nuroa.xml";
$ponteiro = fopen($arquivo, "w");
fwrite($ponteiro, "<?xml version='1.0' encoding='utf-8'?>");
fwrite($ponteiro, '<nuroa>');

$sqlguiatexto = "SELECT id FROM imoveis";
$rsqlguiatexto = mysql_query($sqlguiatexto)
or die ("Não foi possível realizar a consulta ao banco de dados");

while($roweditusertexto = mysql_fetch_array($rsqlguiatexto))
   {

       $conteudo .= '<ad>';
       $conteudo .= '<id><![CDATA['.$roweditusertexto['id'].']]></id>';
       $conteudo .= '</ad>';
       fwrite($ponteiro, $conteudo);

   }

fwrite($ponteiro, '</nuroa>');
fclose($ponteiro);

Can anyone tell me what's going on for this? Strange is that there is no repeated record.

I look forward to helping you, thank you!

    
asked by anonymous 23.02.2016 / 22:33

1 answer

2

You are adding content repeatedly to the file.

See the difference within WHILE :

$arquivo = "nuroa.xml";
$ponteiro = fopen($arquivo, "w");
fwrite($ponteiro, "<?xml version='1.0' encoding='utf-8'?>");
fwrite($ponteiro, '<nuroa>');

$sqlguiatexto = "SELECT id FROM imoveis";
$rsqlguiatexto = mysql_query($sqlguiatexto)
or die ("Não foi possível realizar a consulta ao banco de dados");

while($roweditusertexto = mysql_fetch_array($rsqlguiatexto))
   {
       $conteudo  = '<ad>';
       //        ↑ aqui não vai o ponto, pois é um conteúdo novo.
       $conteudo .= '<id><![CDATA['.$roweditusertexto['id'].']]></id>';
       $conteudo .= '</ad>';
       fwrite($ponteiro, $conteudo);

   }

fwrite($ponteiro, '</nuroa>');
fclose($ponteiro);
    
23.02.2016 / 22:39