2014-01-18 43 views
0

我是新手。我試圖添加頭文件到csv從sqlite3數據庫提取數據,但沒有運氣。任何幫助,將不勝感激。使用php從sqlite3添加頭文件到csv

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

$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('explorer.csv', 'w'); 



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

?> 
+1

好地方是在你的代碼中的'headers'? –

+0

嗨Shankar,查詢... sql select statment是標題right.can你幫我吧..我沒有得到它。 – preethi

回答

0

爲什麼不能做這樣的事情?:

$headers = array('CompanyId', 'DateTime', 'Serial', ...); 
results = $db->query('SELECT ' . implode(',', $headers) . ' FROM table'); 

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

// write headers 
fputcsv($fp, $headers); 

// write data. I would use SQLITE3_NUM. This makes more sense 
while ($row = $results->fetchArray(SQLITE3_NUM)) { 
    fputcsv($fp, $row); 
} 
fclose($fp); 
+0

嗨,我試過你的代碼,我最後得到這個錯誤。 '解析錯誤:語法錯誤,意外的'{'在C:\ ..... \ downnload.php 13號線上' – preethi

+0

抱歉,這是一個錯字 – hek2mgl

+0

謝謝赫克我解決了它.. – preethi