-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.php
More file actions
35 lines (30 loc) · 1.03 KB
/
sort.php
File metadata and controls
35 lines (30 loc) · 1.03 KB
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
<?php
// Database connection
$con = mysqli_connect("localhost", "root", "423301", "train_db");
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
// Fetch all trains sorted by train_name (A-Z)
$result = $con->query("SELECT * FROM trains ORDER BY train_name ASC");
// Prepare JSON output
$output = "";
while ($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($output != "") {
$output .= ",";
}
$output .= '{"id":"' . $rs["id"] . '",';
$output .= '"train_no":"' . $rs["train_no"] . '",';
$output .= '"train_name":"' . $rs["train_name"] . '",';
$output .= '"source":"' . $rs["source"] . '",';
$output .= '"destination":"' . $rs["destination"] . '",';
$output .= '"departure":"' . $rs["departure"] . '",';
$output .= '"arrival":"' . $rs["arrival"] . '",';
$output .= '"status":"' . $rs["status"] . '"}';
}
// Format and print JSON
$output = '{"records":[' . $output . ']}';
echo ($output);
// Close connection
$con->close();
?>