In the previous lecture, we learned:
Until now, our webpages have been static or interactive only inside the browser. However, if we want to store user information such as login details, registrations, articles, products, or orders, we need a database and a server-side programming language.
In this lecture, we will learn:
PHP stands for Hypertext Preprocessor.
PHP is a server-side scripting language used to build dynamic websites and web applications.
Unlike HTML and JavaScript, PHP code executes on the server before the page is sent to the user’s browser.
HTML can only display information.
PHP can:
Executed inside the browser.
Examples:
HTML File
↓
Browser
↓
Display Webpage
Executed on the web server.
Examples:
Browser
↓
Server
↓
PHP Executes
↓
Database
↓
Response Sent Back
A Database is a repository where information is stored in an organized manner.
It allows data to be:
A database may store:
DBMS stands for Database Management System.
A DBMS is software used to create, manage, and maintain databases.
For this course, we will use MySQL.
Before creating databases, we should understand some important concepts.
An Entity is anything in the universe under your consideration that can be identified.
Examples:
If we are building a library system, then:
Book
Student
Author
Publisher
are all entities.
Attributes describe an entity.
Example:
Entity:
Student
Attributes:
Student ID
Name
Email
Age
Gender
Each attribute becomes a column in a database table.
A single row of data is called a Record or Tuple.
Example:
| ID | Name | Age |
|---|---|---|
| 1 | Ali | 22 |
This complete row is one record.
A Table stores related records.
Example:
Students Table
| ID | Name | Age |
|---|---|---|
| 1 | Ali | 22 |
| 2 | Ahmed | 24 |
A relationship connects one table with another.
Example:
Authors
↓
Articles
One author can write many articles.
Database
↓
Tables
↓
Records (Rows)
↓
Attributes (Columns)
↓
Values
Before creating a database, we should design it properly.
This process is called Database Design.
Good database design:
A relational database stores information in multiple related tables.
Instead of placing everything in one table, data is divided logically.
Example:
Authors
↓
Articles
↓
Categories
All tables are connected through keys.
Keys uniquely identify records and create relationships between tables.
A Primary Key uniquely identifies each record.
Characteristics:
Example:
AuthorID
A Foreign Key creates a relationship between two tables.
Example:
AuthorID
↓
Articles Table
The AuthorID in the Articles table refers to the AuthorID in the Authors table.
A Unique Key allows only unique values.
Example:
Email Address
No two users should have the same email address.
A Composite Key consists of two or more columns combined to uniquely identify a record.
Example:
StudentID + CourseID
Together they form one unique key.
MySQL is a Relational Database Management System (RDBMS).
It uses SQL (Structured Query Language) to manage data.
With MySQL, we can:
SQL stands for Structured Query Language.
SQL is the standard language used to communicate with databases.
CREATE DATABASE joinarticle;
This command creates a new database named joinarticle.
USE joinarticle;
This command tells MySQL which database will be used.
Example:
CREATE TABLE authors
(
author_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(150)
);
The ALTER TABLE command is used to modify the structure of an existing table.
Example:
ALTER TABLE authors
ADD phone VARCHAR(20);
Possible uses:
The TRUNCATE TABLE command removes all records from a table but keeps the table structure.
Example:
TRUNCATE TABLE authors;
CRUD represents the four basic operations performed on database records.
| Operation | SQL Command |
|---|---|
| Create | INSERT |
| Read | SELECT |
| Update | UPDATE |
| Delete | DELETE |
These are the most important SQL queries you will use throughout your development career.
Used to add new records.
INSERT INTO table_name
(column1, column2)
VALUES
(value1, value2);
INSERT INTO authors
(first_name, last_name, email)
VALUES
('Ali', 'Khan', 'ali@example.com');
Used to retrieve data.
SELECT first_name, last_name, email
FROM authors;
SELECT *
FROM authors;
Used to modify existing records.
UPDATE authors
SET email='new@email.com'
WHERE author_id=1;
Always use a WHERE clause when updating records. Otherwise, every record in the table may be updated.
Used to remove records.
DELETE FROM authors
WHERE author_id=1;
Always use a WHERE clause when deleting records. Otherwise, all records in the table may be deleted.
Create a table to store user information.
The table should include the following fields:
Insert at least 10 student records into your table.
Use the following format as a guide:
INSERT INTO table_name
(
first_name,
last_name,
email,
password,
gender,
date_of_birth,
registration_date,
country,
address
)
VALUES
(
'Ali',
'Yasin',
'ali@example.com',
'password123',
'Male',
'2002-05-12',
CURDATE(),
'Pakistan',
'Lahore'
);
Retrieve selected columns:
SELECT first_name, last_name, email
FROM table_name;
Retrieve all records:
SELECT *
FROM table_name;
Update the email address or country of one student using the UPDATE query.
Delete a single student record using the DELETE query.
Students should practice the following SQL commands until they become familiar with them:
CREATE DATABASEUSECREATE TABLEALTER TABLETRUNCATE TABLEINSERTSELECTUPDATEDELETEThese SQL commands form the foundation of MySQL and will be used throughout the remainder of the PHP and database modules.
In this lecture, we learned:
In the next lecture, we will connect PHP with MySQL, execute SQL queries from PHP, and build a simple form that stores user information in the database.