main
|
Last change
on this file since 75f74d9 was 75f74d9, checked in by Vlado 222039 <vlado.popovski@…>, 6 months ago |
|
Initial commit: Adding Book Tracker code
|
-
Property mode
set to
100644
|
|
File size:
1.3 KB
|
| Rev | Line | |
|---|
| [75f74d9] | 1 | <?php
|
|---|
| 2 | require_once '../connect.php';
|
|---|
| 3 |
|
|---|
| 4 | header('Content-Type: application/json');
|
|---|
| 5 |
|
|---|
| 6 | try {
|
|---|
| 7 |
|
|---|
| 8 | if (isset($_GET['search'])) {
|
|---|
| 9 | $search = $_GET['search'];
|
|---|
| 10 |
|
|---|
| 11 | $query = "SELECT authorid, firstname, lastname
|
|---|
| 12 | FROM author
|
|---|
| 13 | WHERE firstname ILIKE :search
|
|---|
| 14 | OR lastname ILIKE :search
|
|---|
| 15 | ORDER BY firstname, lastname
|
|---|
| 16 | LIMIT 10";
|
|---|
| 17 |
|
|---|
| 18 | $stmt = $conn->prepare($query);
|
|---|
| 19 | $stmt->execute([':search' => "$search%"]);
|
|---|
| 20 | $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | echo json_encode($results);
|
|---|
| 24 |
|
|---|
| 25 | } else if (isset($_GET['id'])) {
|
|---|
| 26 |
|
|---|
| 27 | $authorId = $_GET['id'];
|
|---|
| 28 |
|
|---|
| 29 | $query = "SELECT * FROM author WHERE authorid = :authorId";
|
|---|
| 30 | $stmt = $conn->prepare($query);
|
|---|
| 31 | $stmt->execute([':authorId' => $authorId]);
|
|---|
| 32 |
|
|---|
| 33 | $author = $stmt->fetch(PDO::FETCH_ASSOC);
|
|---|
| 34 |
|
|---|
| 35 | if(!$author) {
|
|---|
| 36 | throw new Exception('Author not found');
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | echo json_encode($author);
|
|---|
| 40 |
|
|---|
| 41 | } else {
|
|---|
| 42 | throw new Exception('Either search term or author ID is required');
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | } catch(Exception $e) {
|
|---|
| 46 | http_response_code(400);
|
|---|
| 47 | echo json_encode(['error' => $e->getMessage()]);
|
|---|
| 48 | } catch(PDOException $e) {
|
|---|
| 49 | http_response_code(500);
|
|---|
| 50 | echo json_encode(['error' => 'Database error']);
|
|---|
| 51 | }
|
|---|
| 52 | ?> |
|---|
Note:
See
TracBrowser
for help on using the repository browser.