Create JSON API Using PHP and MYSQL

By | November 24, 2015

Now these day’s mobiles are getting more dearer than other gadget’s. APP’s are the important factor for making mobile your favorite gadget. Same as apps now these day’s everyone what their website to be mobile friendly and some of them create app also for their website. Now these days mobile app’s are more common for website owner’s and market for app’s are increasing day by day. Some of the app’s are based on the website and fetch the data from the server. So today we will tell you that how you can create the API of your website data so that you can also create the mobile app with the same data. Today’s article is all about Create JSON API using PHP.

Creating API for your website is quite easy and you can achieve it easily with some basic knowledge of PHP. In this tutorial we will create a simple application.

Step’s to do it.

  1. Create Database and add Dummy text to table

First of all we will create the database with the name of myrestaurant and add table “webcuisine” to it.

When you run the above code it will create a table named webcuisine. After creating the table insert some values to the table by using any form or directly through the mysql commands. Here we will add some of the dummy text for you using the mysql commands.

Now we have inserted some dummy data into the table. You will see that there is a column with “0” value in fourth line this is because we are going to show only those items in the API which are having “1” as status.

Retrieve data using the PHP

We create a function named “gerCuisineList”. Under this function we will retrieve all the list from table¬†which is having 1 as status. If we dont found anything than will send the the result in JSON format so that our device will read it. If we found the details than we will just add the values to an array and return the whole array as JSON.

Here we have to create the array because JSON supports only Array as value.

We will create a class for this function named cuisineAPI.php

So now we have the values now the last part is remaining which is for sending the values to the device. We use POST method to receive the details from mobile to ask for the values from our table.

We will create a getCuisine.php.

So if you call this page you will get the JSON result which you can use with your mobile device.

Hope you like our today’s tutorial “Create JSON API using PHP and MYSQL. if you have any comment or advice please let us know.

One thought on “Create JSON API Using PHP and MYSQL

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.