forked from econpy/torque
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport.php
69 lines (57 loc) · 1.81 KB
/
export.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
require("./creds.php");
// Connect to Database
mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
if (isset($_GET["sid"])) {
$session_id = mysql_real_escape_string($_GET['sid']);
// Get data for session
$output = "";
$sql = mysql_query("SELECT * FROM $db_table WHERE session=$session_id ORDER BY time DESC;") or die(mysql_error());
if ($_GET["filetype"] == "csv") {
$columns_total = mysql_num_fields($sql);
// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
mysql_free_result($sql);
mysql_close($con);
// Download the file
$csvfilename = "torque_session_".$session_id.".csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$csvfilename);
echo $output;
exit;
}
else if ($_GET["filetype"] == "json") {
$rows = array();
while($r = mysql_fetch_assoc($sql)) {
$rows[] = $r;
}
$jsonrows = json_encode($rows);
mysql_free_result($sql);
mysql_close($con);
// Download the file
$jsonfilename = "torque_session_".$session_id.".json";
header('Content-type: application/json');
header('Content-Disposition: attachment; filename='.$jsonfilename);
echo $jsonrows;
exit;
}
else {
exit;
}
}
else {
exit;
}
?>