If you’re a beginner for making RestAPIs for clients, first I will recommend my first tutorialĀ Rest APIs Part-1. In this tutorial, I will show to

  1. How to receive values from clients?
  2. How to validate values?
  3. How to insert values into the database?
  4. How to fetch values from database?
  5. How to send data to clients?

I have a database called db_location. In this database, I have a table called tbl_loc. I have four columnĀ in my table. Now I am creating RestAPIs e.g

	$servername = "localhost";
	$username = "root";
	$password = "";
	$db_name = "db_location";

	// Create connection
	$conn = new mysqli($servername, $username, $password,$db_name);

	// Check connection
	if ($conn->connect_error) {
		outputErrorResponse(405,'Connection failed:');

	$lat = isset($_POST['lat'])?$_POST['lat']:'';
	$lng = isset($_POST['lng'])?$_POST['lng']:'';
	$loc_name = isset($_POST['loc_name'])?$_POST['loc_name']:'';

		outputErrorResponse(422,'latitude is missing');

		outputErrorResponse(422,'Longitude is missing');

		outputErrorResponse(422,'location name is missing');

	// insert data into table

	$sql = 'insert into tbl_loc(lat,lng,loc_name)values("'.$lat.'","'.$lng.'","'.$loc_name.'")';

	// reterived data from table

	$fetch_sql = 'select * from tbl_loc';
	$result = $conn->query($fetch_sql);
	if($result->num_rows == 0){
		outputErrorResponse(404,'No data found');
		$data = array();
		while($row = $result->fetch_assoc()){
			$data[] = $row;

		$output['data'] = $data;

		echo json_encode($data);


	function outputErrorResponse($code, $msg){
		$data = array('coce'=> $code,
					'message' => $msg);
		echo json_encode($data);



The output response will have like this

How to make RestAPIs for sending and retrieve data from database part-2 2


If you still have any confusion in my post, you can see my post video tutorial in Hindi and Urdu

Thanks for reading my post, for further updates, Kindly subscribe my video channel and like my facebook page.