Ishaque Javed Ishaque Javed - 1 month ago 8
PHP Question

how to add custom column in db table when using 'into outfile' query

I am using this query to create CSV of my mysql DB table

$path = getcwd() . '/uploads/data.csv' ;

$sql = "select email,username from `engine4_user` LIMIT 5000 into outfile '$path' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ";


But now I need one extra field that is profile url .which doesn't exists on the table. So i wanna create a virtual url column using the ID of that user table. Like https://mysiteurl/profile/$user_id .

Please help. Thanks in advance.

Ima Ima
Answer

Use "CONCAT" operator

$sql = "SELECT
        email,
        username, 
        CONCAT('https://mysiteurl/profile/',user_id) as profile_url 
        FROM `engine4_user` 
        LIMIT 5000 
        into outfile '$path' FIELDS TERMINATED BY ','  LINES TERMINATED BY '\n' ";

See example here