本文介绍了PHP从SQL Server导出到CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些帮助从SQL Server查询导出到csv。
I need some help to export from a SQL Server query to csv.
我有查询,但是当我获取结果,我需要把它一个变量并导出它。
I have the query but when I'm fetching the result I need to put it on an variable and export it.
这是我所拥有的:
$query = 'select * from sqlserver_table';
$result = odbc_exec($conMsSql,$query);
// this gives me the columns
while(odbc_fetch_row($result)){
$field1 = odbc_result($result, 1);
$field2 = odbc_result($result, 2);
$field3 = odbc_result($result, 3);
$field4 = odbc_result($result, 4);
$field5 = odbc_result($result, 5);
$field6 = odbc_result($result, 6);
$field7 = odbc_result($result, 7);
}
// this is to export
$file = fopen("export.csv","w");
foreach ($list as $line){ // how can I put the result in this variable ($list)?
fputcsv($file,explode(',',$line));
}
fclose($file);
推荐答案
>
You'd want something like this:
$csvName = "export.csv"
$sqlQuery = 'select * from sqlserver_table';
$sqlRresult = odbc_exec($conMsSql, $sql);
$fp = fopen(csvName , 'w');
while ($export = odbc_fetch_array($sqlRresult)) {
if (!isset($headings))
{
$headings = array_keys($export);
fputcsv($fp, $headings, ',', '"');
}
fputcsv($fp, $export, ',', '"');
}
fclose($fp);
这篇关于PHP从SQL Server导出到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!