SlideShare a Scribd company logo
4
Most read
5
Most read
16
Most read
PHP DATABASE MANAGEMENT
Introduction
• Database is one of the most common
application that reside in the server.
• Therefore Common Gateway Interface e.g PHP
provides a link between user-side applications
and server-side applications such Database
Management System e.g. MySQL.
• The main component for storing data in a
system is a DBMS.
Database Connection
PHP 5 and later can work with a MySQL database using:
• MySQLi extension (the "i" stands for improved)
• PDO (PHP Data Objects)
• Earlier versions of PHP used the MySQL extension. However, this
extension was deprecated in 2012.
• Both MySQLi and PDO have their advantages:
• PDO will work on 12 different database systems, whereas MySQLi
will only work with MySQL databases.
• So, if you have to switch your project to use another database, PDO
makes the process easy. You only have to change the connection
string and a few queries. With MySQLi, you will need to rewrite the
entire code - queries included.
• Both are object-oriented, but MySQLi also offers a procedural API.
Connecting to server
• <?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Create a Database
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Create a Database (Example)
<?php
$servername = "localhost";
$username = “root";
$password = ” ";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE school";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
CREATING A TABLE
• <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = “school";
// Create connection
$conn = mysqli_connect($servername,
$username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " .
mysqli_connect_error());
}
// sql to create table
$sql = "CREATE TABLE admission (
id INT(6) AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30),
lastname VARCHAR(30),
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON
UPDATE CURRENT_TIMESTAMP)";
if (mysqli_query($conn, $sql)) {
echo "Table admission created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Insert data
$sql = "INSERT INTO admission(firstname, lastname,
email) VALUES (‘Peter', ‘Juma',
‘pjuma@gmail.com')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
• HTML form to send data to a database via PHP
<body>
<form method=“GET" action= “student.php">
First Name<input name=“fname" value="" />
Last name<input name=“lname" value="" />
E-mail <input name=“email" value="" />
<input type="submit“ value=“ADD”>
</form>
Insert data from HTML form
$fnam=GET[“fname”];
$lnam=GET[“lname”];
$em=GET[“email”];
$sql = "INSERT INTO admission (firstname, lastname,
email) VALUES (‘$fnam', ‘$lnam', ‘$em')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Select data
//list records
$sql = "SELECT id, firstname, lastname FROM admission";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " .
$row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Delete record
Connect to the server
$sql = "DELETE FROM admission WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Update records
Connect to the server
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE
id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Update records (Procedural)
Connect to the server
$sql = "UPDATE admission SET lastname='Doe' WHERE
id=2";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Group Assignment (15 marks)
Develop a web application with the following
features:
• User-side validation/manipulation (JavaScript)
(5 marks)
• Use the application to manage a database
(insert data, update record, delete record and
list records) (10 marks)

More Related Content

Similar to PHP DATABASE MANAGEMENT.pptx (20)

PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PPTX
MySQL with PHP
MsSJeyalakshmiVelsUn
 
PDF
The HyperText Markup Language or HTML is the standard markup language
Lovely Professional University
 
PPTX
This slide show will brief about database handling
averynight005
 
PPTX
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
 
PPTX
CHAPTER six DataBase Driven Websites.pptx
KelemAlebachew
 
PPTX
Unit 4- Working with SQL.pptx
mythili213835
 
PPTX
3-Chapter-Edit.pptx debre tabour university
alemunuruhak9
 
ODP
Php modul-3
Kristophorus Hadiono
 
PPTX
Chapter 3.1.pptx
mebratu9
 
PPTX
3 php-connect-to-my sql
Achchuthan Yogarajah
 
PPTX
Learn PHP Lacture2
ADARSH BHATT
 
DOC
Ex[1].3 php db connectivity
Mouli Chandira
 
PPSX
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
PDF
lab56_db
tutorialsruby
 
PDF
lab56_db
tutorialsruby
 
PPT
Php Mysql
Mudasir Syed
 
PPTX
Php basics
Egerton University
 
PPTX
PHP Database Programming Basics -- Northeast PHP
Dave Stokes
 
PPT
Migrating from PHP 4 to PHP 5
John Coggeshall
 
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
MySQL with PHP
MsSJeyalakshmiVelsUn
 
The HyperText Markup Language or HTML is the standard markup language
Lovely Professional University
 
This slide show will brief about database handling
averynight005
 
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
 
CHAPTER six DataBase Driven Websites.pptx
KelemAlebachew
 
Unit 4- Working with SQL.pptx
mythili213835
 
3-Chapter-Edit.pptx debre tabour university
alemunuruhak9
 
Chapter 3.1.pptx
mebratu9
 
3 php-connect-to-my sql
Achchuthan Yogarajah
 
Learn PHP Lacture2
ADARSH BHATT
 
Ex[1].3 php db connectivity
Mouli Chandira
 
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
lab56_db
tutorialsruby
 
lab56_db
tutorialsruby
 
Php Mysql
Mudasir Syed
 
Php basics
Egerton University
 
PHP Database Programming Basics -- Northeast PHP
Dave Stokes
 
Migrating from PHP 4 to PHP 5
John Coggeshall
 

Recently uploaded (20)

PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PPTX
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
PDF
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
PPTX
Powerful Uses of Data Analytics You Should Know
subhashenia
 
PDF
Optimizing Large Language Models with vLLM and Related Tools.pdf
Tamanna36
 
PPTX
01_Nico Vincent_Sailpeak.pptx_AI_Barometer_2025
FinTech Belgium
 
PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
PDF
Group 5_RMB Final Project on circular economy
pgban24anmola
 
PPTX
apidays Singapore 2025 - Generative AI Landscape Building a Modern Data Strat...
apidays
 
PDF
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
PDF
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
PDF
Research Methodology Overview Introduction
ayeshagul29594
 
PDF
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
PDF
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
PPTX
在线购买英国本科毕业证苏格兰皇家音乐学院水印成绩单RSAMD学费发票
Taqyea
 
PPT
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
Powerful Uses of Data Analytics You Should Know
subhashenia
 
Optimizing Large Language Models with vLLM and Related Tools.pdf
Tamanna36
 
01_Nico Vincent_Sailpeak.pptx_AI_Barometer_2025
FinTech Belgium
 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
Group 5_RMB Final Project on circular economy
pgban24anmola
 
apidays Singapore 2025 - Generative AI Landscape Building a Modern Data Strat...
apidays
 
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
Research Methodology Overview Introduction
ayeshagul29594
 
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
在线购买英国本科毕业证苏格兰皇家音乐学院水印成绩单RSAMD学费发票
Taqyea
 
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
Ad

PHP DATABASE MANAGEMENT.pptx

  • 2. Introduction • Database is one of the most common application that reside in the server. • Therefore Common Gateway Interface e.g PHP provides a link between user-side applications and server-side applications such Database Management System e.g. MySQL. • The main component for storing data in a system is a DBMS.
  • 3. Database Connection PHP 5 and later can work with a MySQL database using: • MySQLi extension (the "i" stands for improved) • PDO (PHP Data Objects) • Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012. • Both MySQLi and PDO have their advantages: • PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. • So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included. • Both are object-oriented, but MySQLi also offers a procedural API.
  • 4. Connecting to server • <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>
  • 5. Create a Database <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Create database $sql = "CREATE DATABASE myDB"; if (mysqli_query($conn, $sql)) { echo "Database created successfully"; } else { echo "Error creating database: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 6. Create a Database (Example) <?php $servername = "localhost"; $username = “root"; $password = ” "; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Create database $sql = "CREATE DATABASE school"; if (mysqli_query($conn, $sql)) { echo "Database created successfully"; } else { echo "Error creating database: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 7. CREATING A TABLE • <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = “school"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
  • 8. // sql to create table $sql = "CREATE TABLE admission ( id INT(6) AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30), email VARCHAR(50), reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)"; if (mysqli_query($conn, $sql)) { echo "Table admission created successfully"; } else { echo "Error creating table: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 9. Insert data $sql = "INSERT INTO admission(firstname, lastname, email) VALUES (‘Peter', ‘Juma', ‘[email protected]')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?>
  • 10. • HTML form to send data to a database via PHP <body> <form method=“GET" action= “student.php"> First Name<input name=“fname" value="" /> Last name<input name=“lname" value="" /> E-mail <input name=“email" value="" /> <input type="submit“ value=“ADD”> </form>
  • 11. Insert data from HTML form $fnam=GET[“fname”]; $lnam=GET[“lname”]; $em=GET[“email”]; $sql = "INSERT INTO admission (firstname, lastname, email) VALUES (‘$fnam', ‘$lnam', ‘$em')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?>
  • 12. Select data //list records $sql = "SELECT id, firstname, lastname FROM admission"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } $conn->close(); ?>
  • 13. Delete record Connect to the server $sql = "DELETE FROM admission WHERE id=3"; if (mysqli_query($conn, $sql)) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 14. Update records Connect to the server $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } $conn->close(); ?>
  • 15. Update records (Procedural) Connect to the server $sql = "UPDATE admission SET lastname='Doe' WHERE id=2"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 16. Group Assignment (15 marks) Develop a web application with the following features: • User-side validation/manipulation (JavaScript) (5 marks) • Use the application to manage a database (insert data, update record, delete record and list records) (10 marks)