我正在尝试使用自定义标头生成CSV,例如USRNAME,USER PROFILE,EMAIL。
这是我正在使用的代码。
$path = getcwd() . '/uploads/data.csv' ;
$sql = "select username, CONCAT('<a href =\"$profile',username,'\">',username,'</a>') as profile_url ,email from `engine4_user` LIMIT 5000 into outfile '$path' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ";
但我不知道如何在DB值之前添加自定义标头或自定义行。请帮我添加这些字段。
I wants a CSV that will out put like:
USERNAME , PROFILE , EMAIL
Jonson1 , http://mysite/profile/jonson1 , jonson.test1@gmail.com
Jonson2 , http://mysite/profile/jonson2 , jonson.test2@gmail.com
Jonson3 , http://mysite/profile/jonson3 , jonson.test3@gmail.com
Jonson4 , http://mysite/profile/jonson4 , jonson.test4@gmail.com
Now I generate :
Jonson1 , http://mysite/profile/jonson1 , jonson.test1@gmail.com
Jonson2 , http://mysite/profile/jonson2 , jonson.test2@gmail.com
Jonson3 , http://mysite/profile/jonson3 , jonson.test3@gmail.com
Jonson4 , http://mysite/profile/jonson4 , jonson.test4@gmail.com
I need to add
USERNAME , PROFILE , EMAIL
提前谢谢你。
答案 0 :(得分:0)
使用php ... Click for Reference
的fputcsv()添加标题答案 1 :(得分:0)
我找到了两种方法。
第一:方法
生成CSV后,我重写了它。
$file = fopen($path,'r+'); //This to open the CSV
fputcsv($file,array(USRNAME , USER PROFILE , EMAIL)); //This to write a new row
第二种方式(通过使用UNION ALL),如下所示:
$sql = "SELECT 'New user name', 'profile name', 'story title', 'story link', 'link to delete article'
UNION ALL
SELECT `engine4_users`.username,
CONCAT(`engine4_users`.username,'[$profile',`engine4_users`.username,']') as profile_url , `engine4_blog_blogs`.title,
CONCAT('$blog_url',`engine4_users`.user_id,'\/',`engine4_blog_blogs`.blog_id) as blog_url ,
CONCAT('$blog_url','delete/',`engine4_blog_blogs`.blog_id) as delete_url
FROM engine4_blog_blogs INNER JOIN `engine4_users` ON `engine4_blog_blogs`.owner_id = `engine4_users`.user_id WHERE `engine4_users`.level_id = 4 AND `engine4_blog_blogs`.creation_date >= '$prevDate' into outfile '$path' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n\r' ";
我正在使用第二种方式确保直接使用查询是最好的方式:)