export2csv.php 1.63 KB
Newer Older
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
<?php
session_start();
if (!isset($_SESSION['userLoggedIn'])) {
	echo "You are not logged in!<br>\n";
	echo "This page should be run from within the admin interface!\n";
	exit;
}else {
	$INCLUDE_DIR = "../..";
	$USER_SETTINGS = parse_ini_file($INCLUDE_DIR . "/scripts/user-settings.ini");
	require $INCLUDE_DIR . "/scripts/wxc_functions.inc";
	@include $INCLUDE_DIR . "/custom.inc";
	
	//downloaded file name
	$file_name = "meshmap_db_export-" . date('Y_m_d') . ".csv";
	
	// export csv
	function exportMysqlToCsv($filename = "meshmap-export.csv") {
	
	//   $conn = dbConnection();
	$conn = wxc_connectToMySQL();
	// Check connection
	    if ($conn->connect_error) {
	        die("Connection failed: " . $conn->connect_error);
	    }
	    $sql_query = "SELECT * FROM node_info";
	
	    // Gets the data from the database
	    $result = $conn->query($sql_query);
	
	    $f = fopen('php://memory', 'wt');
	    $first = true;
	    while ($row = $result->fetch_assoc()) {
	        if ($first) {
	            fputcsv($f, array_keys($row));
	            $first = false;
	        }
	        fputcsv($f, $row);
	    } // end while
	
	    $conn->close();
	
	    $size = ftell($f);
	    rewind($f);
	
	    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	    header("Content-Length: $size");
	    // Output to browser with appropriate mime type, you choose ;)
	    header("Content-type: text/x-csv");
	    header("Content-type: text/csv");
	    header("Content-type: application/csv");
	    header("Content-Disposition: attachment; filename=$filename");
	    fpassthru($f);
	    exit;
	
	}
	// call export function
	exportMysqlToCsv($file_name);
	
}
?>