fputcsv 设置分号分隔符

fputcsv setting semicolon delimiter

我能够获取从存储过程返回的数据并将其设置为数组。

之后,我打开文件路径上传文件。

该文件正在吐出一个带有逗号分隔符的文本文件。

我发现您可以使用 fputcsv 将分隔符设置为分号。

这是我的尝试,从 SP 调用开始:

<?php
  $filename = 'myFile';

  $thecall = mysqli_query($dbc, "CALL SP_SVC($servicecodes)");

  if($thecall){
    $out = array(); 

    while($row = $thecall->fetch_assoc()){
      $out[] = $row;
    }

    $file = fopen("//folderName/IS/business/reports/TXT Report files/$filename.txt", 'w');
    foreach($out as $fields){
      fputcsv($file, explode(';',$fields), ";");
    }
  }
?>

使用上面的方法,我在控制台中收到一条错误消息:

Warning:  explode() expects parameter 2 to be string, array given in 
D:\htdocs\mySite\api\serviceReport.php on line 99

Warning:  fputcsv() expects parameter 2 to be array, null given in 
D:\htdocs\mySite\api\serviceReport.php on line 99

第99行是fputcsv函数所在的位置

在 foreach 循环中,我试图回显 $fields 变量,但出现以下错误:

Notice:  Array to string conversion in D:\htdocs\mySite\api\serviceReport.php on line 99

我做错了什么,如何将分隔符设置为分号?

fputcsv($file, explode(';',$fields), ";");

$fields是数据库中的一行数据,所以不要用explode,直接写出来...

fputcsv($file, $fields, ";");

但是当你读取文件并存储它,然后写入它时,你可以将这两个步骤合二为一以保存临时数组...

$filename = 'myFile';

$thecall = mysqli_query($dbc, "CALL SP_SVC($servicecodes)");

if($thecall){
    $file = fopen("//folderName/IS/business/reports/TXT Report files/$filename.txt", 'w');
    while($row = $thecall->fetch_assoc()){
        fputcsv($file, $row, ";");
    }
    fclose($file);
}