create rest api using php and mysql – onlinecode

create rest api using php and mysql – onlinecode

In this post we will give you information about create rest api using php and mysql – onlinecode. Hear we will give you detail about create rest api using php and mysql – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we are going on how to Create Rest Api Using Php And Mysql. so here We will create REST API with CRUD operations to create a record, read a record, update record and delete the record.

REST stands for Representational State Transfer. It means allows two clients to communicate with a server. the Rest API uses HTTP requests to GET, PUT, POST, and DELETE data.

There are four types of API. such as SOAP, XML-RPC, JSON-RPC. and REST. so here we will use REST API.

Create Database and table

CREATE TABLE IF NOT EXISTS 'register' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'first_name' varchar(64) NOT NULL,
  'last_name' varchar(64) NOT NULL,
  'address' text NOT NULL,
  'email' varchar(64) NOT NULL,
  'mobile' varchar(12) NOT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

connect to database with phpCreate a new connect.php file. The below code is to provide how to connect with the database.The following code is used to connect MySQL from PHP. It requires hostname, database username, database password, and database name.

<?php

 $hostname="localhost";
 $username="root";
 $password="test";
 $database="register";

 $conn = mysqli_connect($hostname,$username,$password,$database); 
 
?>

create new record using rest apiCreate a new add.php file and paste the below code in this file. when you call this file at that time you will use the post method. so here we will get all data using the post method and insert it into the database.

<?php

    $first_name  = $_POST['txtFname'];
    $last_name   = $_POST['txtLname'];
    $address     = $_POST['txtAddress'];
    $email       = $_POST['txtEmail'];
    $mobile      = $_POST['txtMobile'];
 
    include('connection.php');
    $insert="insert into register (first_name,last_name,address,email,mobile) values('$first_name','$last_name','$address','$email','$mobile')";
    	
	
	if (mysqli_query($conn,$insert)) {
		$resultData = array('status' => true, 'message' => "Your record inserted successfully");
	} else {
		$resultData = array('message' => "Unable to create record");
	}
	header('content-type: application/json');
	echo json_encode($resultData);
?>

get all records using rest apiCreate a new getAll.php file and paste the below code in this file. if you want to get all records using rest API then you can see below example.

<?php
    include('connection.php');
 
    $slt="select * from register";
    $rec=mysqli_query($conn,$slt);
    while($row=mysqli_fetch_array($rec))
    {
        $resultData[] = array('first_name' => $row['first_name'], 'last_name' => $row['last_name'],'email'=>$row['email'],'address'=>$row['address'],'mobile'=>$row['mobile']);
    }
	header('content-type: application/json');
    echo json_encode($resultData);
    @mysqli_close($conn);
?>

get single record using rest apiCreate a new single.php file and paste the below code in this file. if you want to get a single record using rest API then you can see below example.

<?php
	include('connection.php');
	$id = $_REQUEST['id'];
	$slt="select * from register where id=$id";
	$rec=mysqli_query($conn,$slt);
	$row=mysqli_fetch_row($rec);
	if (!empty($row)) {
		$resultData = $row;
	} else {
		$resultData = array('message' => "No Data Found");
	}
	header('content-type: application/json');
	echo json_encode($resultData);
?>

Update record using rest apiCreate a new update.php file and paste the below code in this file. if you want to update a record using rest API then you can pass data and particular id(update particular id wise) with post method.

<?php
    $first_name  = $_POST['txtFname'];
    $last_name   = $_POST['txtLname'];
    $address     = $_POST['txtAddress'];
    $email       = $_POST['txtEmail'];
    $mobile      = $_POST['txtMobile'];

    include('connection.php');
    $update="update register set first_name='$first_name',last_name='$last_name',address='$address',email='$email',mobile='$mobile' where id='".$_REQUEST['edit_id']."'";
    if (mysqli_query($conn,$update)) {
		$resultData = array('status' => true, 'message' => "Record Updated successfully");
	} else {
		$resultData = array('message' => "Unable to update record");
	}
	header('content-type: application/json');
	echo json_encode($resultData);
}
?>

Delete record using rest apiCreate a new delete.php file and paste the below code in this file. if you want to delete a record using rest API then you can pass particular id(delete particular id wise) with a post or get method.

<?php
	include('connection.php');

	$delete="delete from register where id='".$_REQUEST['delete_id']."'";

    if (mysqli_query($conn,$delete)) {
		$resultData = array('status' => true, 'message' => "Record Deleted successfully");
	} else {
		$resultData = array('message' => "Unable to delete record");
	}
	header('content-type: application/json');
	echo json_encode($resultData);
?>

Please follow and like us:

Hope this code and post will helped you for implement create rest api using php and mysql – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  21  =  26

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US