Jump to content

PHP OUTFILE with datestamp


SalientAnimal
Go to solution Solved by Jacques1,

Recommended Posts

Hi All,

 

This is part of a PHP script that I am wanting to use to automate some daily data exports. The problem I am having, is adding a datestamp to my export file. The script itself works when executed in MySQL but not under PHP.  Does anyone have any insight into how I can get this working?

The error message I am currently getting is, and the part of php script I am running can be seen below.

 

 

Parse error: syntax error, unexpected '", DATE_FORMAT( NOW(), '%Y%m%d' (T_CONSTANT_ENCAPSED_STRING) in
 

 

$sql1 = "SET @exportfile = 
   CONCAT(SELECT
'FIELD_1'
, 'FIELD_2'
, ....
UNION ALL
SELECT * FROM mydatabasetable INTO OUTFILE 'E:/Data/exports/DailyExport/myexportfile" 
, DATE_FORMAT( NOW(), '%Y%m%d')
, ".csv'
FIELDS TERMINATED BY ','        
        ENCLOSED BY ''''
        LINES TERMINATED BY '\r\n'
    );";


$sql2 = "PREPARE processing FROM @exportfile;";
$sql3 = "EXECUTE processing;";
$sql4 = "DROP PREPARE processing;";

 

Any help here is greatly appreciated.

Edited by SalientAnimal
Link to comment
Share on other sites

You have a a single quote after OUTFILE which does not appear to have a corresponding closing quote

 

The single quote you are referring to has a corresponding single quote after the .csv

 

Although, this does work when run directly on MySQL Workbench, I'm not sure what needs to change for plugging it into PHP.

 

 

This is what the working script in MySQL Workbench looks like:

SET @exportfile = 
   CONCAT("SELECT  
'FILED_1'
,  'FIELD_2'
        ,  ....
UNION ALL
SELECT * FROM mydatabasetable INTO OUTFILE 'E:/Data/exports/DailyExport/myoutputfile" 
, DATE_FORMAT( NOW(), '%Y%m%d')
, ".csv'
FIELDS TERMINATED BY ','        
        ENCLOSED BY ''''
        LINES TERMINATED BY '\r\n'"
    );


PREPARE processing FROM @exportfile;
EXECUTE processing;
DROP PREPARE processing;
Edited by SalientAnimal
Link to comment
Share on other sites

I can't say I am experienced at all in doing out files when it comes to PHP. Doing it under MySQL Workbench is a lot easier, so my question is really the principle of applying it. My understanding in PHP is that the actual query needs to be within Double quotation marks, however this becomes a challenge when taking the existing file / script directly from MySQL Workbench and trying to plug it into PHP. I'm unsure of how to apply this.

That said, I'm going to attempt a change based on what you have said, let me see what the outcome is.

Edited by SalientAnimal
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.