Build Simple REST API with PHP and MySQL

REST stands for Representational State Transfer. RESTful Web services are one way of providing interoperability between computer systems on the Internet. Rest API help to communicate between the client app and the server application. REST is an architecture style for designing networked applications. A REST API defines a bunch of functions which developers can perform requests and receive responses via HTTP protocol such as GET and POST.
In this tutorial, you will learn to how create CRUD (create, read, update, delete) operation REST API with PHP and MySQL. This is a very simple example, you can just copy paste, and change according to your requirement.
Before started to implement the REST API with PHP and MySQL, look files structure:
  • build-simple-rest-api-with-php-mysql
    • class
      • DBConnection.php
      • Student.php.
    • student
      • create.php
      • read.php
      • update.php
      • delete.php
      • .htaccess
Understanding REST API
REST provides a block of HTTP methods which are used to alter the data. The following are common HTTP methods:
  • GET — is used for reading and retrieving data.
  • POST — is used for inserting data.
  • PUT/PATCH — is used for updating data.
  • DELETE — is used for deleting data.
Step 1: Create MySQL Database and Table
For this tutorial, you need a MySQL database with the following table:
Step 2: Connect to Database file named DBConnection.php
The code below shows the database credentials
Step 3: Create class
Create a class file named Student.php inside class/ folder.
  • The Student class handles the CRUD process
  • __construct() — Loads the required DBConnection.
  • createStudent() — Add Student Record.
  • updateStudent() — Update Student Record.
  • getAllStudent() — get Student all Records.
  • getStudent() — get Student single Record.
  • deleteStudent() — delete Student Record.
Step 4: Add Student Record — POST Method
Create PHP file named student/create.php to insert student records to MySQL database. We will check for POST HTTP request and call method createStudent() to insert student data to MySQL database table and return JSON data
In this method createStudent() from class Student.php, we will insert record into student table.


Step 5: Read Student Record from the Database — GET Method
Create PHP file named student/read.php to get student records to MySQL database. We will check for GET HTTP request and call method getAllStudent() or getStudent() to get student data to MySQL database table and return JSON response
In this method getAllStudent() or getStudent() from class Student.php, we will get record(s) into student table.
Get All Student Records


Get Student single Record


Step 6: Update Student Record — POST Method
Create PHP file named student/update.php to update student records to MySQL database. We will check for POST HTTP request and call method updateStudent() to update student data to MySQL database table and return JSON response
In this method updateStudent() from class Student.php, we will update record into student table.


Step 7: Delete Student Record from the Database — GET Method
Create PHP file named student/delete.php to delete student record to MySQL database. We will check for GET HTTP request and call method deleteStudent() to delete student data to MySQL database table and return JSON response
In this method deleteStudent() from class Student.php, we will delete record into student table.


Step 8: Create .htaccess Rewrite Rule with PHP for Clean URLs
Create student/.htaccess file to write some rule to access rest api with pretty URLs. We will add following rules.