使用PHP创建包含表中所有数据(*)的TXT文件

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 file_put_contents function to put the data from my SQLite table into the TXT file.

<?php
$db = new SQLite3('sshTunnel.sqlite');
$results = $db->query('SELECT * FROM mydata');

$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.

try output buffering:

ob_start() ;
var_dump($results) ;
file_put_contents($file, ob_get_clean()) ;