-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.php
102 lines (89 loc) · 2.21 KB
/
extract.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", "UTF-8");
// Validate password
$password = $_POST['password'];
if ($password != "hendiaodemima") {
echo "wrong password";
exit(1);
}
// Connect to database
function connect_db () {
$db = "localhost";
$username = "root";
$password = "BabyBibo1117";
if (!($link = mysql_connect($db, $username, $password))) {
return ("");
}
else {
return ($link);
}
}
// Define filename for download
$filename = "umunc_register_" . "2014_national_" . date('Ymd') . ".csv";
// Define CSV header row
$header = array(
"姓名",
"性别",
"年龄",
"身份证号码",
"在读学校",
"在读年级",
"监护人",
"监护人手机",
"代表类别",
"代表团名称",
"领队姓名",
"指导老师姓名",
"指导老师联系电话",
"代表手机",
"备选手机",
"QQ",
"微信",
"邮箱",
"模联年龄",
"模联履历",
"是否参加过UMUNC会议",
"参加的会议名称",
"委员会志愿",
"注册时间");
// Define HTTP header
header("Content-Encoding: UTF-8");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv; charset=UTF-8");
echo "\xEF\xBB\xBF";
// Begin output process
// Connect to database server
if (!($link = connect_db())) {
echo "connect to database error";
exit(1);
}
// Select database
mysql_select_db("umunc_v2");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
// Begin sql query
$query = "select * from national_2014";
if (!$result = mysql_query($query)) {
print ("query failed " . mysql_error());
exit(1);
}
// Define output stream
$out = fopen("php://output", 'w');
// Write header row to CSV
fputcsv($out, $header);
// write data line by line
while ($row = mysql_fetch_row($result)) {
foreach ($row as $index => $content) {
if(is_numeric($row[$index])) {
$row[$index] = '="' . $content . '"';
}
}
fputcsv($out, $row);
}
fclose($out);
echo "Success. End of output.";
exit(0);
?>