exec("set names utf8"); $sql = "SELECT * FROM Miasta"; $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); foreach ($dbh->query($sql) as $row) { echo $row['Nazwa'] .' - '. $row['IDWojewództwa'] . '
'; $stringData = $row['Nazwa'] .' - '. $row['IDWojewództwa'] . '
'; fwrite($fh, $stringData); } fclose($fh); /*** close the database connection ***/ $dbh = null; } catch(PDOException $e) { echo $e->getMessage(); } ?>