gpuk360 gpuk360 - 3 months ago 6x
SQL Question

Create a TXT File with PHP that contains all Data (*) FROM a Table

I am trying to create a TXT file with PHP. This works fine. Unfortunately, it seems that I do not have the correct syntax, because there is no content inserted using the

function to put the data from my SQLite table into the TXT file.

$db = new SQLite3('sshTunnel.sqlite');
$results = $db->query('SELECT * FROM tunnelthomas');

$file = 'D:\test.txt';
file_put_contents($file, $results);

The file is written, but it contains
0 Bytes


If there are rows, $results will contain an SQLite3Result object. You still need to loop over the result set to get the data.

For example:

while ($row = $results->fetchArray()) {
    file_put_contents($file, implode(', ', $row), FILE_APPEND);

Note that this is just an example, there are dedicated functions to write CSV if you need that.