STEP 1: CREATE A DATABASE
CREATE DATABASE restfulapi; STEP 2: CREATE A TABLE NAMED tbl_other_photos with params:- id, title, download_link and img_url
CREATE TABLE IF NOT EXISTS IF NOT EXISTS `tbl_other_photos` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `download_link` varchar(255) NOT NULL, `image_url` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
STEP 3: Insert dummy values into the database – you can create a real HTML form to collect data but in this tutrial i will manually insert the data into our table
INSERT INTO `tbl_other_photos` (`id`, `title`, `download_link`, `image_url`) VALUES (1, 'Mt View Retreat', 'http//:g-photos.com/retreat', 'http//:placeholder.com/img1.jpg'), (2, 'Summer Games', 'http//:g-photos.com/sports', 'http//:placeholder.com/img2.jpg'), (3, 'Health Meetup', 'http//:g-photos.com/health', 'http//:placeholder.com/img3.jpg');
STEP 4:- CONNECT TO THE DATABASE AND SELECT INSERTED VALUES
<?php include_once "../includes/config.php"; header("Content-type:application/json"); //database configuration $host = "localhost"; $user = "root"; $pass = ""; $database = "restfulapi"; $result = mysqli_query($connect, "SELECT * FROM tbl_other_photos"); while($row = mysqli_fetch_assoc($result)){ $output[]=$row; } print(json_encode($output, JSON_PRETTY_PRINT)); mysqli_close($connect); ?>
Response will be
[ { "id": "1", "title": "Mt View Retreat", "download_link": "http\/\/:g-photos.com\/retreat", "image_url": "http\/\/:placeholder.com\/img1.jpg" }, { "id": "2", "title": "Summer Games", "download_link": "http\/\/:g-photos.com\/sports", "image_url": "http\/\/:placeholder.com\/img2.jpg" }, { "id": "3", "title": "Health Meetup", "download_link": "http\/\/:g-photos.com\/health", "image_url": "http\/\/:placeholder.com\/img3.jpg" } ]