-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
40 lines (34 loc) · 1.13 KB
/
index.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
$servername = "localhost";
$username = "root";
$password = "root";
$dbname="hw4";
$conn = mysqli_connect($servername,$username,$password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
echo "Database Connection Error";
}
$path =$_SERVER['REQUEST_URI'];
$temp = explode('/', $path);
$id= $temp[count($temp)-1];
if(preg_match('/^[0-9]*$/', $id))
{
$query="SELECT Book.title,Book.year,Book.price,Book.category,GROUP_CONCAT(Authors.Author_name) AS Authors from Book,Book_Authors,Authors where Book.Book_id=Book_Authors.Book_id AND Book_Authors.Author_id=Authors.Author_id AND Book.Book_id=$id";
$results=mysqli_query($conn,$query);
while($row1= mysqli_fetch_assoc($results))
{
$entire_list_array[]=$row1; /* fetching row which consists of title,book,price,category,authors based on id */
}
echo json_encode($entire_list_array);
}
else
{
$sql_query="SELECT title FROM Book";
$result=mysqli_query($conn,$sql_query);
while($row = mysqli_fetch_assoc($result))
{
$entire_array[]=$row;
}
echo json_encode($entire_array);
}
?>