-
Notifications
You must be signed in to change notification settings - Fork 3
/
csv.php
38 lines (29 loc) · 1.12 KB
/
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
<?php
error_reporting(E_ALL);
set_error_handler(function ($severity, $message, $file, $line) {
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $file, $line);
}
});
$config = parse_ini_file(__DIR__ . '/config.ini');
$query = rawurldecode($_SERVER['QUERY_STRING']);
if (null === $query) {
die("Error\n\"Must pass a query, eg: {$_SERVER['SCRIPT_NAME']}?select * from table\"");
}
$pdo = new PDO("mysql:dbname={$config['mysql_db']};host={$config['mysql_host']}", $config['mysql_user'], $config['mysql_pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
try {
$data = $pdo->query($query);
$fp = fopen('php://output', 'w');
$columns = [];
for ($i = 0; $i < $data->columnCount(); $i++) {
$columns[] = $data->getColumnMeta($i)['name'];
}
fputcsv($fp, $columns);
while ($row = $data->fetch(PDO::FETCH_NUM)) {
fputcsv($fp, $row);
}
} catch (Exception $e) {
die("Error\n" . preg_replace('/[\'":,;]/', '', preg_replace('/.{50}/', "\\0\n", $e->getMessage())));
}