forked from BoBoLin/Map-Drawer-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_connect.php
64 lines (50 loc) · 1.55 KB
/
db_connect.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
<?php
header("Content-type: application/json; charset=utf-8");
$servername = "";
$username = "";
$password = "";
$dbname = "geo";
$type = $_POST["type"]; //判斷為insert or read
//------- insert to db -------------
if($type == 'insert')
{
$kml_str = $_POST["kml_str"];
$date_str = $_POST["date_str"];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
date_default_timezone_set('Asia/Taipei');
$t = time();
$pretty_date = date("Y-m-d H:i:s", ($t));
$sql = "INSERT INTO kml (id, kml_content, date)
VALUES ('$date_str', '$kml_str', '$pretty_date')";
if ($conn->query($sql) === TRUE) {
//echo "New record created successfully\n";
$status = "ok";
} else {
//echo "Error: " . $sql . "<br>" . $conn->error;
$status = "err";
}
$conn->close();
$data = json_encode(array("error" => $status));
echo "handler(" . $data . ");";
}
//------- read from db -------------
else if($type == 'read')
{
$read_id = $_POST["id"];
mysql_connect("localhost", "root", "diclabisgood");
mysql_select_db("geo");
mysql_query("set name utf8"); //以utf8讀取資料,讓資料可以讀取中文
$data = mysql_query("SELECT * FROM kml WHERE id = '$read_id'");
$rs=mysql_fetch_row($data);
//echo $rs[0]." ";
//echo $rs[2]."<br>";
$data = json_encode(array("kml" => $rs[1]));
//jsonp 方法
//echo "{$_GET['callback']}($data)";
echo "handler(" . $data . ");";
}
?>