preethi
preethi

Reputation: 905

Exporting data from database to csv file using php

I am able to export database to csv but my code somehow imports twice the data to my csv file. I.e same column twice side by side.this is my code. I think my problem is with the implode statment. Any help would be appreciated.

<?php

$db = new sqlite3('I:\preethi\webbs.db');

$headers = array

('Id','CompanyId','DateTime','Serial','DeviceId','AgentAId','GpsAddress','Targa','CommonRoadDescription'

,'RoadCivicNumber','VehicleBrandDescription','VehicleModelDescription' ,'VerbaliVehicleTypeDescription','CommonColorVehicleDescription','VerbaliRuleOneCode','VerbaliRuleOneDes

cription','VerbaliRuleOnePoints'  

    ,'VerbaliClosedNoteDescription','Points','VerbaliMissedNotificationDescription 

','MissedNotificationNote','StatementNote');

    $results = $db->query('select'.implode (',',$headers).'from VerbaliData');

    //$results = $db->query( 'select 
Id   ,CompanyId  ,DateTime  ,Serial  ,DeviceId  ,AgentAId  

,GpsAddress  ,Targa  ,CommonRoadDescription  ,RoadCivicNumber  ,VehicleBrandDescription  

,VehicleModelDescription  ,VerbaliVehicleTypeDescription  ,CommonColorVehicleDescription  

,VerbaliRuleOneCode  ,VerbaliRuleOneDescription  ,VerbaliRuleOnePoints  ,VerbaliClosedNoteDescription  

,Points  ,VerbaliMissedNotificationDescription  ,MissedNotificationNote  ,StatementNote  from 

VerbaliData');


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

    fputcsv($fp,$headers);

    while ($row = $results->fetchArray()) {
             fputcsv($fp, $row);
    }

    fclose($fp);

    ?>

Upvotes: 0

Views: 606

Answers (2)

Gaurav Joseph
Gaurav Joseph

Reputation: 946

You have a slight prob in your code fetchArray() returns two array sets one associative and one is numbered, use fetchArray(SQLITE3_NUM) or fetchArray(SQLITE3_ASSOC).

Upvotes: 0

Jenson M John
Jenson M John

Reputation: 5689

Just try with :

while($row = $results->fetchArray(SQLITE3_NUM)) {

Or

while($row = $results->fetchArray(SQLITE3_ASSOC)) {

More Details: http://php.net/manual/en/sqlite3result.fetcharray.php

Upvotes: 1

Related Questions