-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
81 lines (73 loc) · 1.67 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
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/*
Export
Western Washington University
Service Learning Center Database
*/
include 'database.php';
$user = 'gebauee_writer';
$pass = 'A2AWvYYCR';
$host = 'db.cs.wwu.edu';
$dbname = 'db_slc1410';
$database = mysqli_connect($host, $user, $pass, $dbname) or die("Error: " . mysqli_error($connection));
//Post the file name to outname
//File name will be 'output.csv' if not posted
$outname = $_POST['outname'];
if ($outname) $filename = $outname . ".csv";
else $filename = "output.csv";
//Header info
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
//Post the SQL query to export
if (isset($_POST['export']))
{
//Query the database
$query = $_POST['export'];
$query = str_replace('\\', '', $query);
$sql = mysqli_query($database, $query) or die("Error: " . mysqli_error($database));
$columns = $sql->field_count;
//Write field names to the first row
for ($i = 0; $i < $columns; $i++)
{
$heading = $sql->fetch_field_direct($i)->name;
echo $heading;
echo ",";
}
echo "\n";
//Write each row
if($_POST["exportKeyword"])
{
$rowIndex = 0;
$exportRows = $_POST["exportRows"];
}
while ($row = mysqli_fetch_array($sql))
{
if ($_POST["exportKeyword"])
{
if (in_array(strval($rowIndex), $exportRows)){
for ($i = 0; $i < $columns; $i++)
{
echo $row[$i];
echo ",";
}
echo "\n";
}
}
else
{
for ($i = 0; $i < $columns; $i++)
{
echo $row[$i];
echo ",";
}
echo "\n";
}
if($_POST["exportKeyword"])
{
$rowIndex = $rowIndex + 1;
}
}
//Echo the table
echo $output;
}
?>