-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetCities.php
More file actions
37 lines (27 loc) · 1022 Bytes
/
Copy pathgetCities.php
File metadata and controls
37 lines (27 loc) · 1022 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
<?php
require_once 'assets/config/config.php';
$db_options = [
/* important! use actual prepared statements (default: emulate prepared statements) */
PDO::ATTR_EMULATE_PREPARES => false
/* throw exceptions on errors (default: stay silent) */
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/* fetch associative arrays (default: mixed arrays) */
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$pdo = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
$stmt = $pdo->prepare('SELECT name FROM demo_cities WHERE state_id = :state_id');
$stmt->execute(['state_id' => $_GET['id']]);
$cities = $stmt->fetchAll(PDO::FETCH_ASSOC);
output($cities);
/*
* After converting data array to JSON send back to javascript using
* this function.
*/
function output($output)
{
http_response_code(200);
try {
echo json_encode($output, JSON_THROW_ON_ERROR);
} catch (JsonException) {
}
}