-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.php
More file actions
40 lines (32 loc) · 811 Bytes
/
Search.php
File metadata and controls
40 lines (32 loc) · 811 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 = "test2";
$user = "postgres";
$password = "";
$searchTerm = $_POST['city'];
// 创建数据库连接
$conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
// 检查连接是否成功
if (!$conn) {
echo "Failed to connect to the database";
exit;
}
// 执行查询
$query = "SELECT DISTINCT \"area unit\" FROM joinedAuckland WHERE \"region\" = '$searchTerm'";
$result = pg_query($conn, $query);
if (!$result) {
echo "Query failed";
exit;
}
// 构建结果数组
$options = array();
while ($row = pg_fetch_assoc($result)) {
$options[] = $row['area unit'];
}
// 输出查询结果作为 JSON
echo json_encode($options);
// 释放结果集和关闭连接
pg_free_result($result);
pg_close($conn);
?>