Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7812735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:36:32+00:00 2026-06-02T04:36:32+00:00

/* generate csv content */ while($res = mysql_fetch_assoc($query)) { /* If two or more

  • 0
/* generate csv content */

while($res = mysql_fetch_assoc($query))         {
        /* If two or more columns of the result have the same field names, the last column will take precedence. */
        for($i=0; $i<mysql_num_fields($query);$i++)             {
                $data = $res[mysql_field_name($query,$i)];
                $data = trim( preg_replace( '/\s+/', ' ', $data ) );
                echo "\"".$data."\";";
        }
        echo "\r\n";
}

The following code intends to remove extra whitespace or newline characters and generate a csv file. My problem is that on some workstations the csv file is displayed correctly and on other stations it is displayed incorrectly although all stations have the same Regional and Language Settings.

Is there something else I should check out or use a different code for generating proper csv files that cleans hidden spaces or new lines?

LATER EDIT

I discovered some faulty settings regarding regional and language settings options, although the settings we’re the same, in Regional Options > Customise > Numbers > List Separator it was something different from “;” so this explains some of the issues reported.

The fix is to implement proper csv code generator and check workstation configurations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:36:33+00:00Added an answer on June 2, 2026 at 4:36 am

    Try this:

    $outfp = fopen('php://output', 'w');
    
    while ($row = mysql_fetch_assoc($query)) {
      foreach ($row as &$field) {
        $field = preg_replace('/\s+/', ' ', trim($field));
      }
      fputcsv($outfp, $row, ';');
    }
    

    Using fputcsv() will produce much better output than anything you can write manually, considering many edge cases that your code does not. You can write it to php://output and it will be the same as echoing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml schema and csv data to generate corresponding xml files. I
I have a script that generates a csv file using the following code: header('Content-type:
I have 2 csv files: output.csv output1.csv output.csv has a 5 columns of titles.
I have an url /reportcsv that generates a plain text csv with Content-type: text/csv
I generate csv files with fputcsv and it works just fine, but when I
I generate csv files with fputcsv and it works just fine, but when I
I used Python to generate a CSV file. But when I open it in
I'm creating a bash script to generate some output from a CSV file (I
I have a model in App Engine that I am exporting in CSV format
On a http website, I have loads of csv files stored which chronological naming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.