In this section, we will start building a practical REST API using PHP and MySQL. This API will later be connected with a React Native mobile application.
We will build a Student Management REST API.
The API will support:
This project will use:
The basic architecture will be:
React Native Mobile App
↓
REST API
↓
PHP Backend
↓
MySQL
Database
The mobile application sends HTTP requests to the PHP API.
The backend is the server-side part of the application.
In this project, PHP will process requests from the mobile application and communicate with MySQL.
Mobile App
↓
PHP
↓
MySQL
MySQL will store student information.
For example:
students
id
name
email
mobile
course
PHP will communicate with this database using PDO.
The API is the communication layer between the mobile application and the database.
React Native
↓
API
↓
PHP
↓
MySQL
The mobile application does not directly connect to MySQL.
PHP is commonly used for server-side web development and can be used to create REST APIs.
PHP can receive HTTP requests, validate data, communicate with MySQL, and return JSON responses.
MySQL is a relational database management system that can store structured application data.
For our project, it will store student records.
Student
---------
ID
Name
Email
Mobile
Course
PDO provides a consistent way for PHP applications to communicate with databases.
We will use PDO to connect PHP to MySQL and execute database queries.
PDO also supports prepared statements, which help protect database queries from SQL injection when used correctly.
REST APIs commonly exchange structured data using JSON.
Example:
{
"id": 1,
"name": "Rahul Kumar",
"course": "ADCA"
}
React Native can easily process JSON responses.
For local development, you can use a PHP development environment such as XAMPP.
The basic components are:
Create a project folder inside your local web server directory.
Example:
htdocs/
└── student_api/
The exact location depends on your PHP development environment.
Our project can start with a simple structure.
student_api/
│
├── config/
├── api/
├── index.php
└── database.php
We will expand this structure as the project becomes larger.
The config folder can contain configuration-related files.
student_api/
└── config/
└── database.php
The database connection can be kept separate from API endpoint logic.
The API folder can contain endpoint files.
student_api/
└── api/
├── students.php
├── login.php
└── register.php
Each endpoint can perform a specific API operation.
The API needs a database connection before it can read or write student records.
A PDO connection can be created using:
$pdo = new PDO(
"mysql:host=localhost;dbname=student_api",
"root",
""
);
In a real project, database credentials should be configured securely.
An endpoint is a URL through which a client communicates with the API.
Example:
http://localhost/student_api/api/students.php
This endpoint can be used for student-related operations.
A GET request can be used to retrieve student records.
GET
http://localhost/student_api/api/students.php
The API can query MySQL and return the student records as JSON.
A POST request can be used to create a new student.
POST
http://localhost/student_api/api/students.php
The client can send student information as JSON.
A PUT request can be used to update an existing student.
PUT
http://localhost/student_api/api/students.php?id=1
The API receives the updated information and modifies the database record.
A DELETE request can be used to remove a student.
DELETE
http://localhost/student_api/api/students.php?id=1
The API can delete the corresponding database record.
React Native
↓
HTTP Request
↓
PHP API
↓
Validate Request
↓
MySQL
↓
JSON Response
↓
React Native
This is the basic flow of our application.
Before connecting the API to React Native, test the endpoints using Postman.
For example:
GET /students.php
POST /students.php
PUT /students.php?id=1
DELETE /students.php?id=1
Check the status code and JSON response for every request.
The PHP API should return structured JSON responses.
Example:
{
"success": true,
"message": "Student added successfully",
"data": {
"id": 1,
"name": "Rahul Kumar"
}
}
The API should also return useful error responses.
Example:
{
"success": false,
"message": "Student not found"
}
The HTTP status code should also represent the result appropriately.
The API should validate data before inserting or updating database records.
For example:
A REST API should be designed with security in mind.
Security will be covered in more detail in later lessons.
MySQL Database
↓
PHP + PDO
↓
REST API
↓
Postman Testing
↓
React Native
↓
Mobile Application
Each layer has a specific responsibility in the application.
In the upcoming lessons, we will build the project step by step.
In this project, PHP will provide REST API endpoints, MySQL will store student data, PDO will handle database communication, JSON will be used for data exchange, and Postman will be used to test the API. Later, the same API will be connected to a React Native mobile application.
React Native
↓
REST API
↓
PHP
↓
PDO
↓
MySQL
Question: Which technology will be used to store student data in this PHP REST API project?