0

I am working on a project and new to PHP and I need to know how to export the table data from SQLite to Excel.

I am able to do it from database but I do not know how to export to Excel using PHP.

<?php
$db = new sqlite3('I:\Preeti\explor\WebMobility.db');

$results = $db->query('SELECT * FROM VerbaliData');
while ($row = $results->fetchArray()) {
    var_dump($row);
}
?>
1

1 Answer 1

1

The easiest method whould be to write your results into a CSV file which opens in Excel in a readable format.

See here for more information: http://php.net/fputcsv

$fp = fopen('file.csv', 'w');

while ($row = $results->fetchArray()) {
    fputcsv($fp, $row);
}
fclose($fp);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Beittil, It worked is it possible to add headers
Yeah, sure. You could use the same method to write a line with the names of the columns. You can either take these from the resultset, or write an array with custom headers to the CSV file. In this case you need to make sure that the array matches the amount of columns from the db.
Hi Beittil I tried it but its not working what code should i add more.. while ($row = $results->fetchArray(sqlite3_BOTH); can you help me with this please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.