0% found this document useful (0 votes)
21 views

Chapter 3 Database Connectivity Ver 28 Mac 2023

This document provides an overview of database connectivity in PHP, including: 1. Explaining how to connect a PHP application to a MySQL database using mysqli_connect() and performing basic CRUD operations like insert, select, update, and delete. 2. Detailing the steps to connect to a database, insert data through coding and a web form, retrieve, update, and search data from the database. 3. Stating that students will learn how to install, configure PHP database connectivity, and manipulate data in the database.

Uploaded by

Nur Syahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Chapter 3 Database Connectivity Ver 28 Mac 2023

This document provides an overview of database connectivity in PHP, including: 1. Explaining how to connect a PHP application to a MySQL database using mysqli_connect() and performing basic CRUD operations like insert, select, update, and delete. 2. Detailing the steps to connect to a database, insert data through coding and a web form, retrieve, update, and search data from the database. 3. Stating that students will learn how to install, configure PHP database connectivity, and manipulate data in the database.

Uploaded by

Nur Syahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

CHAPTER 3

DATABASE
CONNECTIVITY

Prepared by:
Mdm Nurul Nisa binti Mohd Nasir
Learning Outcome
CLO1:
Construct the PHP program structure, file and
directory handling, database connectivity
appropriately in developing dynamic web page
that connected to MySQL database ( P4, PLO3 )

CLO3:
Display the ability to visualize the development
process web application ( P3, PLO6 )
Summary
This topic is an introduction to database server
application that deals with the advantages and
disadvantages of database server applications

Students learn how to install, configure and


create PHP database connectivity, and
manipulate data in database by implementing
the code CRUD (create, read, update & delete)
operations.
3.1 DATABASE SERVER

MySQL is an open source relational database


management system
It includes the SQL server and client programs
for accessing the server
Widely used by web application developers,
together with PHP and APACHE
It is pronounced My-es-que-el (Not My-Sequel)
WHY LEARN MY SQL
Leading open source RDBMS
Ease of use – No frills
Fast
Robust
Security
Multiple OS support
Free
Technical support
Support large database– up to 50 million rows, file size limit up to
8 Million TB
SQL SERVER
Microsoft SQL Server is a Relational Database Management System
(RDBMS) designed to run on multiple platforms
Can support thousands of concurrent users
Comes with a number of tools to help database administration and
programming tasks
SQL Server is much more robust and scalable than a desktop
database management system such as Microsoft Access. (can.t
have too many data and cannot work concurrently)
Although SQL Server can also be run as a desktop database system,
it is most commonly used as a server database system.
3.2 DATABASE
CONNECTIVITY WITH
SCRIPTING LANGUAGE
In order to make a system works well with all
components, forms, process, and database
need to be integrated
This is to let the system, know the route and
establish the connection, and which database
to use in the system because there may be a
number of database exist in that particular
folder.
DATABASE CONNECTIVITY
1) To connect to MySQL, the PHP mysqli_connect() function requires
three arguments.
mysqli_connect() function returns TRUE if the connection succeeds,
or FALSE if the attempt fails

Syntax:

mysqli_connect("hostname", "username", "password", "databasename)

OR
mysqli_connect("hostname", "username", "password","databasename")
or die("Could not connect to MySQL");
DATABASE CONNECTIVITY
Syntax: mysqli_connect("hostname", "username", "password", "databasename")

Hostname : name of computer in which MySQL is installed. If the MySQL


database is on the same computer as your website, you can use
localhost as the computer name. If you leave the information black (“ “).
PHP assume localhost
Username : Name of any valid MySQL account. By default username in
”root”
Password : Password specify by the username. If the MySQL account
doesn’t require any password, leave the password blank (“ “)
Databasename: name of databse in PHPMyAdmin
DATABASE CONNECTIVITY
Syntax: mysqli_connect("hostname", "username", "password", "databasename")

You can choose between this 2 types of connection coding


1) $connect = mysqli_connect("localhost", "admin", "a1", "mydb")

2) $hostname = "localhost";
$username = "admin";
$password = "a1";
$dbase= "mydb"
$connect =
mysqli_connect($hostname,$username,$password,$dbase)
1) Connection to database
➢Step 1 : Database
Create Database in XAMPP
Create Table in Database
Create Column and assign data type and length.
➢Step 2: Connect to database
Connection and select database
SQL command
1) Connection to database
<?php
$hostname="localhost";
$username="root";
$password="";
$databasename="mydb";

$conn= mysqli_connect ($hostname, $username, $password,


$databasename) OR die ("Failed to connect to MYSQL");
?>
2) Insert Data
Syntax :
INSERT INTO table_name (column1, column2, column3,...)VALUES (value1,
value2, value3,...);

Example:
$sql = “INSERT INTO pelajar (No_Matrik, nama) VALUES (‘2000, ‘Arif
Najmi’)”
2) Insert Data
Insert data directly through coding
➢Step 1 : Database
Create Database in XAMPP
Create Table in Database
Create Column and assign data type and length.
➢Step 2: Sent data to database
Connection and select database
SQL command
Insert Data (through coding)
STEP 1
Insert Data (through coding)
STEP 2
<?php
$servername = "localhost";
$username = "root";
$password = " ";
$dbname = "student";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO studentdetail (student_Name, student_Matric, student_Email) VALUES ('John', '11DDT12F1090', '[email protected]')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Insert Data (from web form)
➢Step 1 : Database
Create Database in XAMPP
Create Table in Database
Create Column and assign data type and length.
➢Step 2 : Create Web Form
Create form
➢Step 3: Sent data to database
Connection and select database
SQL command
Insert Data from web form
STEP 1
Insert Data (through coding)
STEP 2 (save as add_form.html)
<html>
<body>
<h1>PLEASE ENTER YOUR DETAIL</h1>
<form method="post" action="add.php">
<table>
<tr><td>NAME : </td><td><input type="text" name="student_Name"></td></tr>
<tr><td>MATRIC NO. : </td><td><input type="text" name="student_Matric"></td></tr>
<tr><td>EMAIL : </td><td><input type="text" name="student_Email"></td></tr>
<tr><td></td><td><input type="submit" value="ADD STUDENT"></td></tr>
</table>
</form>
</body>
</html>
Insert Data (through coding)
STEP 3 (save as add.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$student_name=$_POST['student_Name'];
$student_matric=$_POST['student_Matric'];
$student_email=$_POST['student_Email'];

$sql = "INSERT INTO studentinfo (student_Name,student_Matric,student_Email) VALUES ($student_name, $student_matric, $student_email)";


if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
3) Retrieve Data
Syntax :
SELECT * FROM table_name”;

Example:
$sql = “SELECT * FROM student”;
3) Retrieve Data
➢Step 1 : Database
Create Database in XAMPP
Create Table in Database
Create Column and assign data type and length
Insert data into Table

➢Step 2: Retrieve data from database


Connection and select database
SQL command
Retrieve Data
STEP 1
Retrieve Data
STEP 2 (save as retrieve.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT student_Name, student_Matric, student_Email FROM studentdetail";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{ // output data of each row
while($row = mysqli_fetch_assoc($result))
{
echo "Name: " . $row["student_Name"]. " &nbsp;&nbsp;&nbsp;&nbsp; Matric No: " . $row["student_Matric"]. "&nbsp;&nbsp;&nbsp;&nbsp;
Email : " . $row["student_Email"]. "<br>";
}
} else
{
echo "0 results";
}
mysqli_close($conn);
?>
4) Update Data
Syntax :
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Example:
$sql = “UPDATE studentdetail
SET student_Matric='nurul123', student_Email = '[email protected]'
WHERE student_Name='nurul'”;
4) Update Data
➢Step 1 : Database
Create Database in XAMPP
Create Table in Database
Create Column and assign data type and length
Insert data into Table

➢Step 2: Update data from database


Connection and select database
SQL command
Update Data
STEP 1
Update Data
STEP 2 (save as update.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE studentdetail SET student_Matric='nurul123', student_Email = [email protected]' WHERE student_Name='nurul'";
$result = mysqli_query($conn, $sql);
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
5) Search Data
Syntax :
SELECT column_name(s)
FROM table_name
WHERE column_name operator value

Example:
$sql = “SELECT student_Matric
FROM studentdetail
WHERE student_Matric='nurul123'”;
5) Search Data
➢Step 1 : Database
Create Database in XAMPP
Create Table in Database
Create Column and assign data type and length
Insert data into Table

➢Step 2: Retrieve data from database


Connection and select database
SQL command
Search Data
STEP 1
Search Data
STEP 2 (save as search.php)
<<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}$sql = "SELECT student_Name, student_Email FROM studentdetail WHERE student_Matric='12345'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "NAME: " . $row["student_Name"]. " &nbsp;&nbsp;&nbsp;&nbsp; Email: " . $row["student_Email"]."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
6) Delete Data
Syntax :
DELETE FROM table_name
WHERE some_column = some_value

Example:
$sql = “DELETE FROM studentdetail
WHERE student_Matric='12345'”;
6) Delete Data
➢Step 1 : Database
Create Database in XAMPP
Create Table in Database
Create Column and assign data type and length
Insert data into Table

➢Step 2: Delete data from database


Connection and select database
SQL command
Delete Data
STEP 1
Delete Data
STEP 2 (save as delete.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql = "DELETE FROM studentdetail WHERE student_Matric='12345'";
$result = mysqli_query($conn, $sql);

if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
7) Sort Data
Syntax :
SELECT column_name(s)
FROM table_name
ORDER BY column_name

Example:
$sql = “SELECT *
FROM studentdetail
ORDER BY student_Matric";
7) Sort Data
➢Step 1 : Database
Create Database in XAMPP
Create Table in Database
Create Column and assign data type and length
Insert data into Table

➢Step 2: Retrieve and sort data from database


Connection and select database
SQL command
Sort Data
STEP 1
Sort Data
STEP 2 (save as sort.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT student_Name, student_Matric, student_Email FROM studentdetail ORDER BY student_Matric";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{ // output data of each row
while($row = mysqli_fetch_assoc($result))
{
echo "Name: " . $row["student_Name"]. " &nbsp;&nbsp;&nbsp;&nbsp; Matric No: " . $row["student_Matric"]. "&nbsp;&nbsp;&nbsp;&nbsp; Email : " .
$row["student_Email"]. "<br>";
}
} else
{
echo "0 results";
}
mysqli_close($conn);
?>
TUTORIAL 1
1) Create database in XAMPP.
- database name (any name u like)
- column name (any name u like)

2) Create form file :


- Mobile Phone Form
- Mobile Name
- Mobile Model
- Storage / Capacity (radio ) :16GB, 32GB, 64GB, 128GB
- Color

3) Create PHP file to store data in DB.


- SQL : Insert into syntax
TUTORIAL 2
By referring to your answer in Tutorial 1,

1) Retrieve all the data in your database. (hint:Select)

2) Update one of your color data.

3) Delete one of your 16GB data.


THANK YOU

You might also like