Carriage return SQL + PHP + CSV

Solved
Coutcout86 Posted messages 219 Status Member -  
 Anonymous user -
Bonjour,

I am tearing my hair out because of a carriage return problem. I want to export data from an SQL database to a CSV file, but in my database, I have TEXT fields. And in these TEXT fields, I have carriage returns. You might tell me to do a str_replace to remove the BR or \r\n, but the problem is that in my database, my TEXT field is inserted like this:

There are 2 EI requalified as de facto companies.
The employee is the son of one of the 2 managers and will not remain in the event of a sale.


So at the end of the first line, I have neither <br/> nor \n, so when I export this to CSV, it creates a new line for each carriage return.

How could I solve this problem? Does anyone have any ideas?

Thank you in advance.

2 answers

Anonymous user
 
Hello

A CSV field can very well contain a line break, it poses no problem if we respect the CSV conventions which state that if a field contains certain special characters (including line breaks), it must be enclosed in double quotes.
How do you handle the export?
How do you know the import?
If you have a problem, it means that at least one of these two steps is not being done according to the CSV format.
One should not believe that CSV is only fields separated by commas; the CSV format defines some other rules without which it does not function.
0
Coutcout86 Posted messages 219 Status Member
 
I am exporting in PHP this way in my code:

 echo monChamps.";".monDeuxiemeChamps.";" etc..


And what do you mean by 'How do you know the import?'

Thank you in advance.
0
Anonymous user
 
Due to a typo, I meant to write "how do you do the importation?"

That said, it doesn't work because what you're doing is NOT CSV. Imagine there's a semicolon in a field, what will happen during re-reading?
To create CSV, there are built-in functions in PHP:
fputcsv and fgetcsv
0
Coutcout86 Posted messages 219 Status Member
 
It turns out that it writes in a new cell unfortunately...

I import via a SQL query which is then stored in a PHP array.
Indeed, I'm not doing 100% CSV by looking at these functions.

However, could these functions solve my problem?
0
Anonymous user
 
Indeed, I don't do CSV 100%
Let's rather say that you don't do CSV at all.

Yes, these functions really allow you to create CSV. I sometimes use them, and I've never had any problems.

You said earlier that you perform the export using "echo," but echo doesn't write to a file in principle. Given the questions you're asking, I doubt that you're actually redirecting the standard output. So, are you really redirecting the standard output, or if not, how do you actually export?
Similarly: I import via an SQL query, that doesn't mean much; an SQL query was never meant to import data from a CSV file. So how do you really?
0