1

This is a sample table of my original table USER_DETAILS, i want to export the user_id and user_phone fields to a csv file for further implementation in my project.

user_id    user_phone

1          9977660050
2          9977660051
3          9977660042
4          9977660080

P.S.: Please answer me the query for this, instead of giving abrupt answers and suggestions. i guess the table is quite clear to understand.

2
  • the person who downvoted my question is humbly requested to specify the correct reason for doing so. It is very rude to downvote without a reason. Commented Mar 22, 2011 at 3:01
  • 2
    Freak often people don't want to waste their time telling the OP that they could simply have done a simple google search for the answer How to output csv with mysql. Even SO prompts you to look at the other similar questions when you typed in your question Commented Mar 22, 2011 at 3:04

3 Answers 3

3

If you don't really need this to be in PHP, just run:

SELECT user_id, user_phone INTO OUTFILE '/your/filepath'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM user_details;

Obviously, replace '/your/filepath' with the path to the file you want to save to.

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

4 Comments

Didn't know you could do this. +1
Wow, that is cool, I didn't know mysql could do that either. :)
hey @jacob and @masterz - i dont know why did someone downvote my question even if you people find it so interesting and cool. can you please help me get my reputation back?
@Coding-Freak Just give a good answer or something, you only lost 2 rep.
1

Not sure if you want to do this in PHP or just output it to a CSV file. Here's how to do the latter:

SELECT user_id, user_phone
FROM user_details
INTO OUTFILE '/tmp/user_details.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

1 Comment

sorry, your answer is correct but i got the same answer from @zerocates 1 minute before you did. so i had to mark it correct.
0

SELECT 'User','User Phone'

UNION

SELECT user_id,user_phone INTO OUTFILE 'users.csv'

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

FROM USER_DETAILS;

Comments

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.