Before creating tables and building our PHP REST API, we need a MySQL database to store the application data. In this lesson, we will create the student_api database that will be used by our Student Management REST API project.
A database is an organized collection of data. Applications use databases to store information so that it can be created, retrieved, updated, and deleted.
Our Student Management API will store student information inside a MySQL database.
React Native
↓
PHP REST API
↓
MySQL Database
A mobile application may work with a large amount of information. That data needs to be stored somewhere permanently.
For our project, the database will store:
MySQL is a relational database management system. It stores data inside tables made up of rows and columns.
Our PHP REST API will communicate with MySQL to perform database operations.
For our Student Management API, we will create a database named:
student_api
This database will contain the tables required by our REST API.
If you are using a local development environment such as XAMPP, start Apache and MySQL.
Then open phpMyAdmin from your local server environment.
phpMyAdmin provides a graphical interface for managing MySQL databases.
In phpMyAdmin, open the database creation section and enter the name:
student_api
Then create the database.
The database name should match the name used by our PHP connection.
$dbname = "student_api";
If the PHP code uses a different database name, the connection will not point to the database we created.
A MySQL database can also be created using SQL.
CREATE DATABASE student_api;
This SQL statement creates a database named student_api.
After creating the database, we can select it using:
USE student_api;
This tells MySQL that subsequent table operations should be performed inside the selected database.
You can check available databases using:
SHOW DATABASES;
The result should contain:
student_api
After selecting the database, you can check which database is currently selected.
SELECT DATABASE();
The result should show:
student_api
A database can contain multiple tables.
student_api
│
├── students
├── users
└── courses
We will create the required tables in the next database lessons.
The main table for our project will store student information.
students
id
name
email
mobile
course
The table will be created after the database is ready.
Think of the database as a container for related tables.
Database
↓
Tables
↓
Rows
↓
Columns
Our student_api database will contain the data required by the REST API.
PHP needs the database name when creating the PDO connection.
$host = "localhost";
$dbname = "student_api";
$username = "root";
$password = "";
These values are examples for a local development environment and may be different on another server.
The database name becomes part of the PDO Data Source Name.
$dsn = "mysql:host=localhost;dbname=student_api;charset=utf8mb4";
This tells PDO which MySQL database should be used.
PHP
↓
PDO
↓
MySQL Server
↓
student_api
↓
Tables
The PHP REST API will use this connection to work with the database.
The complete SQL command for creating our database is:
CREATE DATABASE student_api;
After executing this command, MySQL creates the database.
To avoid an error when the database already exists, you can use:
CREATE DATABASE IF NOT EXISTS student_api;
This creates the database only when it does not already exist.
Use a consistent database name throughout the project.
student_api
The same name should be used in the PHP configuration.
$dbname = "student_api";
After creating the database, look at the database list in phpMyAdmin.
You should see:
student_api
Click the database to open it. Initially, it may not contain any tables.
Immediately after creation, the database may be empty.
student_api
↓
No Tables Yet
This is normal. Tables will be created in the next step of the project.
The REST API will use the database to store and retrieve information.
React Native
↓
REST API
↓
PHP
↓
student_api
↓
students table
You can use the SQL section of phpMyAdmin or another MySQL client to execute the database creation statement.
CREATE DATABASE student_api;
After execution, refresh the database list to verify the result.
Check the MySQL service and SQL statement when database creation fails.
If the database already exists, running the normal CREATE DATABASE statement may produce an error.
You can use:
CREATE DATABASE IF NOT EXISTS student_api;
This prevents an error caused by the database already existing.
You can verify the database using SQL commands.
SHOW DATABASES;USE student_api;SHOW TABLES;
At this stage, SHOW TABLES may return an empty result because we have not created any tables yet.
Create MySQL Database
↓
Select Database
↓
Create Tables
↓
Insert Data
↓
PHP REST API
↓
Postman
↓
React Native
This is the overall development sequence for our project.
At the end of this lesson, our project should have the following database:
Database:
student_api
Current Status:
Database created
Tables: Not created yet
The next lesson will focus on creating the required database tables.
The first database step in our PHP REST API project is creating the student_api database. This database will contain the tables used by our Student Management API. PHP will later connect to this database using PDO.
MySQL
↓
student_api
↓
Database Tables
↓
PHP REST API
↓
React Native
Question: What is the name of the MySQL database we are creating for the Student Management REST API?