0

I am connecting to a SQLite database. I use the following query to get the results:

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

Now, I want to add tabs and line breaks from the row array using file_put_contents in PHP:

while ($row = $results->fetchArray(SQLITE3_ASSOC))
{
    file_put_contents($file, $row, FILE_APPEND).PHP_EOL;
}

The results are displayed unformatted in one line:

TestTestTest   Test   Test   Test

I wish to have this structure:

Test   Test   Test
Test   Test   Test

Where is the mistake?

sjantke
  • 605
  • 4
  • 9
  • 35

2 Answers2

1
while ($row = mysqli_fetch_row($results))
{
   foreach($row as $value)
   {
     $value .= '|';//use what ever char you what to append
     file_put_contents($file, $value, FILE_APPEND | LOCK_EX);
   }
   file_put_contents($file, '\n', FILE_APPEND | LOCK_EX);
}
bluepinto
  • 165
  • 9
  • Almost. It is still in one line with some errors. Result: Test, Test, Test, \nTest , Test , Test ... – sjantke Jun 29 '16 at 09:31
  • Update: The line break works with "\n" instead of '\n'. For every row iteration, there are (I do not know why) 4 more tabs added. So with 2 rows, there are 8 tabs. For 3 rows, there are 12 tabs. WTF? :-) – sjantke Jun 29 '16 at 09:41
1

I fixed the problem. trim() made it.

while ($row = $results->fetchArray(SQLITE3_ASSOC))
{
    foreach ($row as $value)
    {
        file_put_contents($file, trim($value) . ", ", FILE_APPEND | LOCK_EX);
    }
    file_put_contents($file, "\n", FILE_APPEND | LOCK_EX);
}
sjantke
  • 605
  • 4
  • 9
  • 35