The easiest way to export mysql query to CSV in PHP

Possible duplicate:
PHP code to convert a MySQL query to a CSV file

What is the best and cleanest way to export a mysql query to CSV (for Excel 2003 and later)? The "best" from the point of view is tested, maintained, configured ... therefore, the ideal solution would be something like the standard PHP library .

It should correctly handle the shell, escaping, encoding, cr-lf, field names on the 1st line, ... etc.

There were a few questions about this here in SO (like this one ), but no one focuses on the properties I require above.

Some of my ideas:

  • use fputcsv ? (but encoding, escaping, and cr-lf , it would seem, could not be installed !)
  • extract code from phpmyadmin ...?
  • SELECT * INTO OUTFILE does not work on my server (access denied)
  • system("mysqldump ...") impossible (secure hosting mode)
+4
source share
1 answer

As you say, fputcsv pretty limited for your requirements. Of course, extracting from phpMyAdmin is possible ( export logic is here), but you may have more success using a dedicated library. Here are a few options:

  • php-csv-utils . It is lightweight, customizable, and looks to fit easily into most applications.
  • File_CSV from PEAR, not enough documentation, but at least in 2011 it was released.

Without using any of them, I would not want to select it, but based on the documentation and ease of use, I would choose the first.

Optional: I just noticed this SO question . Charles answered as creepy as mine. I thought I should contact him here!

+1
source

Source: https://habr.com/ru/post/1386839/


All Articles