Skip to content
Advertisement

Extract datas with ‘,’ from database to csv file

This is my code to extract datas from database to csv file. It’s working fine but now that I put it in my program where the datas have addresses, thus having commas ( , ), I’m having problem in printing it.

This is how it looks like:

JavaScript

It should be like this:

JavaScript

The City and email are pushed to the other column. I searched the internet but all I saw was on imporitng datas with commas ( , ).

Plus, the warning and error message in php is also exporting inside the csv file. I don’t problem that much because once I fix this, I think there will be no warning.

Then this is my code:

JavaScript

Advertisement

Answer

I am not sure if you have this already working. But I made a little change to your code.

Here is the code:

JavaScript

I changed to following.

  1. I add a delimiter variable at the top of the script. By this you can swith easy between the delimiter, so if it change from , to ; you can do that by just editing 1 line.
  2. Instead of the all rows I used a for loop since every table is just a number.
  3. Add a check to see in the string contains the delimiter and if so put qoutes around it
  4. Check if the $i is 21 because then we need to go to the new line.
  5. Add exit when there is not db connection or no database can be selected

If this does not work please let me know I will fix it. Notice I haven’t tried this code my self.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement