Simple MySQL to CSV

I needed to make a quick export earlier this week so that we could get records into another program. At first I though I’d need to find a php library to make a csv for me (to save me the time of making the script myself) but it terns out that the process couldn’t be easier. The csv format is very easy to create and really seems to follow several key rules. First, you put things in quotation marks. If it’s a number you don’t have to, but just to be safe I put everything in quotation mark (it was also easier coding wise). Second, if there is a quotation in a cell you must put another quotation before it. Finally, you must create a new line at the end of every new like (obviously), With That in mind I made this:


The code is pretty self explanatory, just doest what you need to as mentioned in the first paragraph. I tested it (admittedly in a limited fashion) and it works great. I don’t think there could be an potential problems but as this was created in less than 5 minutes as really just a quick test I’m not sure. If anybody find anything that might cause issues let me know and I’ll expand upon it but as of now it does exactly what I created it for.

Leave a Reply

Your email address will not be published. Required fields are marked *