-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_unit.php
More file actions
41 lines (32 loc) · 955 Bytes
/
get_unit.php
File metadata and controls
41 lines (32 loc) · 955 Bytes
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
<?php
$host = "localhost";
$port = "5432";
$dbname = "Crime-Data";
$user = "postgres";
$password = "";
$searchTerm = $_POST['city'];
// 创建数据库连接
$conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
// 检查连接是否成功
if (!$conn) {
echo json_encode(array("error" => "Failed to connect to the database"));
exit;
}
$query = "SELECT DISTINCT \"area unit\" FROM merged_violent WHERE \"ta2014 nam\" = '$searchTerm'";
$result = pg_query($conn, $query);
// 检查查询结果
if (!$result) {
echo json_encode(array("error" => "Query execution failed"));
exit;
}
$options = array();
while ($row = pg_fetch_assoc($result)) {
$options[] = $row["area unit"];
}
// 释放结果集和关闭连接
pg_free_result($result);
pg_close($conn);
// 构造响应数据
$response = array("options" => $options);
// 将结果转换为 JSON 格式并发送响应
echo json_encode($response);