1

I'm assigned with a task of creating an automated backup for a database. I used a php event and I want to save my all my backups by a unique name. So I used a date_format and following is my code.

SELECT * FROM redcap_data ;
OUTFILE ' ", DATE_FORMAT(now(),'%Y%m%d_%H%i'),".csv';
FIELDS TERMINATED BY ',' ;
OPTIONALLY ENCLOSED BY '"';
LINES TERMINATED BY "\n";

But it gives an error saying that there is an error in your SQL syntax; near 'OUTFILE ', DATE_FORMAT(now(),'%Y%m%d_%H%i'), .csv''

please help me to find where my error is.

2
  • please check your double quotes Commented Aug 13, 2015 at 6:44
  • you need to use cron for this please check the following answer stackoverflow.com/a/3341381/2191252 Commented Aug 13, 2015 at 6:45

1 Answer 1

0

Try as below

    SET @sql_text = 
   CONCAT (
       "SELECT * FROM `tbl_user` into outfile '/xampp/htdocs/mysite/reports-"
       , DATE_FORMAT( NOW(), '%Y%m%d')
       , ".csv'"
    );

PREPARE s1 FROM @sql_text;
EXECUTE s1;
DROP PREPARE s1;

Refer from Rename outfile with date in mysql

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

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.