$xml="\n\t\t"; while($res = mysqli_fetch_array($row)){ if($res['img']!=""){ $xml .="\n\t\t"; $xml .= "".$res['title']."\n\t\t"; $xml .= "\n\t\t"; $xml.="\n\t"; } } $xml.="\n\r"; $xmlobj=new SimpleXMLElement($xml); $xmlobj->asXML("config.xml"); ?>