Create RESTful API using PHP and MySQL

REST stands for Representational State Transfer, it is a software architectural style for handling information over the internet.
API stands for Application Programming Interface, it is a group of functions and processes that allow the creation of applications that access the features or data of an operating system, application, or other services.
REST API is a need because this is the lightest way to perform CRUD (create, read, update, delete) operations by different applications. In this tutorial, we will explain to you how to create RESTful API with CRUD operation using PHP and MySQL.
So let’s implement RESTful API using PHP and MySQL. look folders and files structure:
  • create-rest-api-using-php-mysql
    • class
      • DBConnection.php
      • Employee.php.
    • emp
      • create.php
      • read.php
      • update.php
      • delete.php
      • .htaccess
A REST API should use the basic HTTP methods like GET, POST, PUT, DELETE. These method are used according to the requirement:
  • GET — retrieve records from database.
  • POST — insert new record in the database.
  • PUT/PATCH — update existing record in the Database.
  • DELETE — delete record from database.
Step 1: Database design and table
Run the following script against MySQL to create the database employee table.
Insert sample data into the employee table.
Step 2: Database configuration
Create a class file named DBConnection.php inside “class/” folder. You should change your username, password and database name
Step 3: Create class
Create a class file named Employee.php inside “class/” folder.
Step 4: Read Employees
Create a read.php file to read employees list from MySQL database table employee. Call method read() and return all employees list and also single employee if passed employee_id param . We will return employees records as response JSON format.
Create method read() in class Employee.php to fetch employee records from MySQL database table.
We will use the REST API with a tool like Postman. Read employee list http://localhost/create-rest-api-using-php-mysql/emp/read http://localhost/create-rest-api-using-php-mysql/emp/read/1
Step 5: Add Employee
Create a create.php file to create employee into MySQL database table employee. Call method create() and handle POST data insert records into MySQL database. We will return response JSON format.
Create method create() in class Employee.php to save employee record into MySQL database table.
We will use the REST API with a tool like Postman. insert POST values
Step 6: Update Employee
Create update.php file to update employee record. We will call method update() from class Employee.php and handle employee update functionality through POST method data.
Create method update() in class Employee.php to update employee record to the MySQL database.
We will use the REST API with a tool like Postman. update values using the POST method
Step 7: Delete Employee
Create a file named delete.php to handle employee delete functionality. We will call method delete() from class Employee.php and handle employee delete functionality.
Create method delete() in class Employee.php to delete employee record from MySQL database.
We will use the REST API with a tool like Postman. Delete value using GET method http://localhost/create-rest-api-using-php-mysql/emp/delete/1