-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_csv.php
47 lines (36 loc) · 1.17 KB
/
export_csv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
require_once('./util/mysqli.php');
$out_dir = './data/safe/';
$database = 'hackathon';
$list_tables_sql = "SHOW TABLES IN $database";
$result = f_mysql_query($list_tables_sql);
while($row = mysqli_fetch_array($result)){
$table = $row[0];
$output_file = "$out_dir$table.safe.csv";
echo "Exporting $database $table to $output_file\n";
export_db_table($database,$table,$output_file);
}
// export_db_table('hackathon','travis211','./data/travis211.safe.csv');
function export_db_table($database,$tablename,$outfile_name){
$query = "SELECT * FROM $database.$tablename";
$result = f_mysql_query($query);
if (!$result) die('Couldn\'t f etch records');
$headers = $result->fetch_fields();
$col_names = [];
foreach($headers as $header) {
$col_names[] = $header->name;
}
if(file_exists($outfile_name)){
echo "$outfile_name already exists... doing nothing\n";
exit();
}
$fp = fopen($outfile_name, 'w');
if ($fp && $result) {
fputcsv($fp, array_values($col_names));
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
}else{
echo "could not open the file or get the mysql result\n";
}
}