Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3284996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:13:02+00:00 2026-05-17T20:13:02+00:00

Im just getting started with PHP, and I need to export a table from

  • 0

Im just getting started with PHP, and I need to export a table from my sqlite database to a CSV or ideally XLS. I have found an example using mysql, but i cant convert it to work with sqlite.

Here is what i have so far:

<?php
$db     = new PDO('sqlite:../ccm.sqlite');
$query  = $db->query('SELECT * FROM Emails');
$export = sqlite_query ($query);
$fields = sqlite_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ ){
    $header .= sqlite_field_name( $export , $i ) . "\t";
}

while( $row = sqlite_fetch_row( $export ) ){
//sqlite_fetch_row doesnt actually exist...
    $line = '';
    foreach( $row as $value ){                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) ){
            $value = "\t";
        }else{
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

if ( $data == "" ){
    $data = "\n(0) Records Found!\n";                        
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=emails.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>

Can anyone help me out with this?
or if there is a simpler way that would be great.
Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:13:03+00:00Added an answer on May 17, 2026 at 8:13 pm

    You are mixing PDO methods with sqlite_* functions; try using one or the other:

    $db = new PDO("sqlite:../ccm.sqlite");
    $query = $db->query("select * from emails");
    $first_row = true;
    while ($row = $query->fetch(PDO::FETCH_ASSOC))
    {
      if ($first_row)
      {
        // I'm not sure how to get the field names using a PDO method but
        // we can use the first row's (or any row's) key values as these
        // are the field names.
        $first_row = false;
        $number_of_fields = count($row);
        $field_names = array_keys($row);
        $first_field_name = $field_names[0];
      }
      // do stuff here with the row
      print_r($row);
    }
    

    or

    $db = sqlite_open("../cmm.sqlite");
    $query = sqlite_query($db, "select * from emails");
    $number_of_fields = sqlite_num_fields($query);
    $first_field_name = sqlite_field_name($query, 0);
    while ($row = sqlite_fetch_array($query))
    {
        // do stuff here with the row.
        print_r($row);
    }
    

    I’m not 100% sure but I think PDO works with sqlite3 databases and sqlite_* functions works with sqlite2 databases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just getting started using MVC in ASP.NET, I'm going to have it so users
I am just getting started with Silverlight and have recently added a Silverlight project
Just getting started on iPhone dev today and have run through Apple's HelloWorld tutorial:
Just getting started with Powershell and have a quick question. Trying to run this
I am just getting into OOP and framework design. I have started by using
Just getting started with DB4O object oriented database. I'm very familiar with SQL, if
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
I am just getting started with flex and am using the SDK (not Flex
I'm just getting started with learning about Unit testing (and TDD in general). My
I'm just getting started with Custom User Controls in C# and I'm wondering if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.