Carriage return SQL + PHP + CSV
Solved
Coutcout86
Posted messages
219
Status
Member
-
Anonymous user -
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:
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.
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
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.
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.
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?
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?
And what do you mean by 'How do you know the import?'
Thank you in advance.
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
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?