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.";
}