0

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);

    ?>
4
  • "imports twice the data to my csv file" or "imports twice the data to my db"? Commented Jan 20, 2014 at 9:33
  • exporting data from sqlite 3 to csv.. but instead it exports twice like ('Id','ID','CompanyId', 'comapnayID','DateTime','datetime','serial','Serial' Commented Jan 20, 2014 at 9:38
  • Can you just copy paste output CSV data in pastebin.com? Commented Jan 20, 2014 at 9:44
  • done. titles sql to csv. headers appearing once but data twice. Commented Jan 20, 2014 at 9:54

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

@preethi Ok I'll check. It would be better if you could provide CSV pastebin URL.
With SQLITE3_NUM or SQLITE_ASSOC It should have worked. If that still didn't work, Just use array_unique($row) before fputcsv($fp, $row);
2 errors :Warning: SQLite3::query(): Unable to prepare statement: 1, near "selectId": syntax error in C:\xampp\htdocs\download.php on line 11 Fatal error: Call to a member function fetchArray() on a non-object in C:\xampp\htdocs\download.php on line 18
@preethi Can you provide table dump & full code. I'll have a look on it.
0

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

Comments

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.