-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetAvailable.php
40 lines (39 loc) · 1.28 KB
/
getAvailable.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
<?php
// Connects to DB and gets array of available cars.
require_once __DIR__ . '/db_config.php';
// connecting to db
$link = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$sql = "SELECT *FROM auto";
$result = mysqli_query($link, $sql);
// check for empty result
if ($result->num_rows) {
$response[""] = array();
while ($row = mysqli_fetch_array($result)) {
$aut = array();
$aut["registracija"] = $row[0];
$aut["marka"] = $row[1];
$aut["model"] = $row[2];
$aut["km"] = $row[3];
$aut["stete"] = $row[4];
$aut["stanjeTanka"] = $row[5];
$aut["thumbimg"] = $row[6];
$aut["kategorija"] = $row[7];
array_push($response[""], $aut);
}
// echoing JSON response
$data = json_encode(array_values($response));
$data = str_replace("]]", "]", $data);
$data = str_replace("[[", "[", $data);
echo $data;
} else {
$response["success"] = 0;
$response["message"] = "No cars found";
echo json_encode($response);
}
?>