outputting fields in a CSV

I am trying to loop through the fields in my database using the following code but nothing is being output

[php]

$fp = fopen(realpath(‘./’) .’/’. file_directory_path() .’/auto-csv/’. $filename, ‘w+’);
      $data = db_result(db_query(“SELECT * FROM {actionmedicalresearch_gdpr_optin}”));
      foreach ($data as $fields) {
        fputcsv($fp, $fields);
      }

[/php]

Please advise

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/2/feed