mysqlからcsvを作ろう

try {
	$dbh = new PDO('mysql:host=localhost;dbname=hoge;charset=utf8','hoge','hoge', array(PDO::ATTR_EMULATE_PREPARES => false));
} catch(PDOException $e){
	exit('データベース接続失敗。'.$e->getMessage());
}

$file_path = "../s3/article.csv";
$export_csv_title = ["id","login_id","role","name","password","mail","test_mail","updated_person","created_at","updated_at"];
$export_sql = "select * from articles";

foreach($export_csv_title as $key => $val){
	$export_header[] = mb_convert_encoding($val, 'SJIS-win', 'UTF-8');
	}

	if(touch($file_path)){
		$file = new SplFileObject($file_path, "w");

		$file->fputcsv($export_header);
		$stmt = $dbh->query($export_sql);

		while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
			$file->fputcsv($row);
		}
		$dbh = null;

	}

echo "finish!!!!!!";

おいおい、ちょっと待てこれ。来てる!

次は、csvをs3に格納する。