PHP 从 SQL 服务器导出为 CSV
PHP Export to CSV from SQL Server
我需要一些帮助才能将 SQL 服务器查询导出到 csv。
我有查询,但是当我获取结果时,我需要将它放在一个变量上并将其导出。
这是我的:
$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);
假设这是您的 sql,您将执行以下操作以将数据从 mssql 导出到 .csv。
$sql = "SELECT * FROM target_database_table_name";
$results = mssql_query($sql, $db);
//Generate CSV file - Set as MSSQL_ASSOC as you don't need the numeric values.
while ($l = mssql_fetch_array($results, MSSQL_ASSOC)) {
foreach($l AS $key => $value){
//If the character " exists, then escape it, otherwise the csv file will be invalid.
$pos = strpos($value, '"');
if ($pos !== false) {
$value = str_replace('"', '\"', $value);
}
$out .= '"'.$value.'",';
}
$out .= "\n";
}
mssql_free_result($results);
mssql_close($db);
// Output to browser with the CSV mime type
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=table_dump.csv");
echo $out;
你想要这样的东西:
$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);
我需要一些帮助才能将 SQL 服务器查询导出到 csv。
我有查询,但是当我获取结果时,我需要将它放在一个变量上并将其导出。
这是我的:
$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);
假设这是您的 sql,您将执行以下操作以将数据从 mssql 导出到 .csv。
$sql = "SELECT * FROM target_database_table_name";
$results = mssql_query($sql, $db);
//Generate CSV file - Set as MSSQL_ASSOC as you don't need the numeric values.
while ($l = mssql_fetch_array($results, MSSQL_ASSOC)) {
foreach($l AS $key => $value){
//If the character " exists, then escape it, otherwise the csv file will be invalid.
$pos = strpos($value, '"');
if ($pos !== false) {
$value = str_replace('"', '\"', $value);
}
$out .= '"'.$value.'",';
}
$out .= "\n";
}
mssql_free_result($results);
mssql_close($db);
// Output to browser with the CSV mime type
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=table_dump.csv");
echo $out;
你想要这样的东西:
$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);