How to save the while result to a txt file? [closed]

-2

Generate txt, but save only the last entry in the database and in case you need all the data.

$querymail = mysql_query("select mat,nome from usuario ");

while($data = mysql_fetch_array($querymail)) {
    $log = str_pad($data[0], 10, "0", STR_PAD_LEFT);
    $log1 = str_pad($data[1], 40, " ", STR_PAD_RIGHT);

    $linhas = "log1$log2.";         
}
    
asked by anonymous 20.05.2017 / 20:47

1 answer

1

The command file_put_contents writes a string to a file, if this file does not already exist it creates the file

    while($data = mysql_fetch_array($querymail)) {
       $log = str_pad($data[0], 10, "0", STR_PAD_LEFT);
       $log1 = str_pad($data[1], 40, " ", STR_PAD_RIGHT);

        $linhas .= $log. " " .$log1."\n";         
    }

file_put_contents('arquivo.txt', $linhas);
  

If you just want to add a value to a file already created, you will have to use a third parameter with the value FILE_APPEND, thus:

file_put_contents('arquivo.txt', $linhas, FILE_APPEND);
    
20.05.2017 / 21:27