sjantke
sjantke

Reputation: 595

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

Upvotes: 0

Views: 282

Answers (2)

jeroen
jeroen

Reputation: 91762

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.

Upvotes: 2

Juan Tomas
Juan Tomas

Reputation: 5183

try output buffering:

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

Upvotes: 0

Related Questions