0

I have a SQL file that have some query in it i want to create a CSV file from this query. Current SQL file included the following code:

SELECT
    ID AS 'Donor ID',
    directorsComments AS 'Director Comments',
    external_donor_id AS 'Donor Number',
    CASE
        WHEN is_approved = '1'
            THEN 'Approved'
        ELSE 'Not Approved'
    END AS 'Approval Status',

    donor_status AS 'Donor Status',
    screening_status AS 'Screening Status',
    Title AS 'Title',
    firstName AS 'First Name',
    lastName AS 'Last Name',

    dateStarted AS 'Created At',
    dateSubmitted AS 'Last Updated',
    ipAddress AS 'IP',

1 Answer 1

3

This can be done directly without PHP using the SELECT ... INTO OUTFILE FIELDS TERMINATED BY ',' command : http://dev.mysql.com/doc/refman/5.7/en/select-into.html

Sign up to request clarification or add additional context in comments.

4 Comments

maybe also LINES TERMINATED BY '\n' ( is this the default ? )
@amdixon: yes, I forgot that - first day back after Christmas - could also add the ENCLOSED BY '"' option as well
so all i need to add these parameters with my SELECT query? I was trying like this.. $list = array ( "Peter,Griffin,Oslo,Norway", "Glenn,Quagmire,Oslo,Norway", ); $file = fopen("contacts.csv","w"); foreach ($list as $line) { fputcsv($file,explode(',',$line)); }
That's correct, there is an example in the link I gave you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.