Exporting table from Amazon RDS into a csv file

前端 未结 6 1367
后悔当初
后悔当初 2020-11-29 17:43

I have a mysql database running in Amazon RDS, and I want to know how to export an entire table to csv format. I currently use mysql server on Windows to query the Amazon da

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 18:17

    I'm using Yii Framework on EC2 connecting to RDS mySQL. The key is to use fputcsv(). The following works perfectly, both on my localhost as well as production.

    $file = 'path/to/filename.csv';
    $export_csv = "SELECT * FROM table";
    
    $qry = Yii::app()->db->createCommand($export_csv)->queryAll();
    
    $fh = fopen($file, "w+");
    foreach ($qry as $row) {
        fputcsv($fh, $row, ',' , '"');
    }
    fclose ($fh);
    

提交回复
热议问题