create rest api using php and mysql
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
1 2 3 4 5 6 7 8 9 | 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 php
Create 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.
1 2 3 4 5 6 7 8 9 10 | <?php $hostname="localhost"; $username="root"; $password="test"; $database="register"; $conn = mysqli_connect($hostname,$username,$password,$database); ?> |
create new record using rest api
Create 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?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 api
Create 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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 api
Create 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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 api
Create 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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 api
Create 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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); ?> |