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

Lab-6 by Anees

This document outlines a lab assignment for interfacing PHP with MySQL, including objectives, tasks, and assessment rubrics. It details three main tasks: performing database operations using MySQLi, implementing CRUD operations via forms, and using PDO for database interactions. The document also includes code snippets for each task and guidelines for report submission and evaluation criteria.

Uploaded by

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

Lab-6 by Anees

This document outlines a lab assignment for interfacing PHP with MySQL, including objectives, tasks, and assessment rubrics. It details three main tasks: performing database operations using MySQLi, implementing CRUD operations via forms, and using PDO for database interactions. The document also includes code snippets for each task and guidelines for report submission and evaluation criteria.

Uploaded by

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

INTERFACING PHP

WITH MYSQL

LAB # 06

Spring 2025

CSE-403L
Database Management System Lab

Submitted by: Anees Ur Rehman


Registration No.: 22PWCSE2168
Class Section: ‘B’

“I affirm that I have completed this work with integrity”

Student Signature: _______________


Submitted to:
Engr. Sumayyea Salahuddin
Friday, May 16, 2025

Department of Computer Systems Engineering


University of Engineering and Technology, Peshawar
Database Management Systems Lab

LAB ASSESSMENT RUBRICS

Unsatisfac- Stu-
Exemplary Acceptable Developing
tory dent
Score
Dimension out of
10 8 6 4 10
Marks

Overall Report is complete, Report is com- Report is mostly Report is in-


Impression of well written, and orga- plete, briefly writ- complete, loosely complete,
Lab Report nized appropriately ten, and organized. written, and fairly sloppy and/or
with additional ele- There are few organized. There disorganized.
ments that enhance it. spellings and/or are spellings and/ There are many
There are no spellings grammar errors. or grammar errors. spellings and
or grammar errors. grammar errors
that affect clar-
ity.

Submission Report is submitted on Report is submit- Report is submit- Report was


time. ted within 24 ted within 72 more than 3
hours of due date. hours of due date. days overdue.

Specification Programs work and ex- Programs work Programs work Programs work
ceed specifications. and meet all speci- and meet partial but fail to meet
fications. specifications. any specifica-
tions.

Verbal Answered clearly and Answered clearly Answered some- Answered


Communica- accurately with suffi- and accurately what clearly and wrongly and in-
tion and cient knowledge. with average somewhat accu- accurately with
Understand- knowledge. rately with limited no knowledge.
ing knowledge.

Output Fig- All the output figures Most of the output Few of the output Output figures
ures/Graphics and graphics are shown figures and graph- figures and graph- and graphics are
clearly and labeled. ics are shown ics are shown shown clearly
clearly and la- clearly and la- and not labeled.
beled. beled.

Marks: (_______+_______+_______+_______)/5= _______

Teacher Remarks and Signature: ____________________________________________________


Interfacing PHP With MySQL

Objectives:
This lab aims at the understanding of:
 Type of MySQL Drivers in PHP
 PHP-MySQL Interfaces

Lab Tasks:
Task 1: Complete Example 3 by providing a suitable query, its result, and then freeing re-
sult variable.

Code:
<?php
// Step 1: Database Connection + Database Selection + Check Connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "task_4_5";

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

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
echo "</br>";
echo "</br>";
?>

<!DOCTYPE html>
<html>
<head>
<title>MySQLi OO Basics</title>
</head>
<body>
<?php
// Step 2: Perform a query or operation on the database
// Query to join member and entry tables and fetch details for a specific year
$year = 2012;
$query = "SELECT m.FirstName, m.LastName, e.TourID, e.Year FROM member m
JOIN entry e ON m.MemberID = e.MemberID WHERE e.Year = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $year);
$stmt->execute();
$result = $stmt->get_result();

// Step 3: Show result


if ($result->num_rows > 0) {
echo "<table border='1'><tr><th>First Name</th><th>Last Name</th><th>Tour
ID</th><th>Year</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["FirstName"] . "</td><td>" . $row["LastName"] . "</
td><td>" . $row["TourID"] . "</td><td>" . $row["Year"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}

// Free the result set


$result->free();
?>
</body>
</html>

<?php
// Step 4: Close Database
$conn->close();
?>

Output:

Task 2: Write PHP-MYSQLI Object-Oriented interface script that connects with one of the
tables of your DBMS Lab Project (For instance, user table or accounts table or any of your
choice). Perform the following:
 Apply the insert command on the table. Take the data from user via HTML Form con-
structs.
 Apply the update command on the table. Take the data from user via HTML Form
constructs.
 Apply the delete query on the table. Take the data from user via HTML Form con-
structs.
 Apply the select query on the same table to retrieve data. Show the data in HTML
Form Constructs.

Code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "task_4_5";

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

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully<br/>";

// Insert data
if (isset($_POST['insert'])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];

$stmt = $conn->prepare("INSERT INTO member (FirstName, LastName) VALUES


(?, ?)");
$stmt->bind_param("ss", $firstName, $lastName);
$stmt->execute();
echo "New records created successfully<br/>";
}

// Update data
if (isset($_POST['update'])) {
$memberID = $_POST['memberID'];
$firstName = $_POST['firstName'];

$stmt = $conn->prepare("UPDATE member SET FirstName = ? WHERE MemberID


= ?");
$stmt->bind_param("si", $firstName, $memberID);
$stmt->execute();
echo "Record updated successfully<br/>";
}

// Delete data
if (isset($_POST['delete'])) {
$memberID = $_POST['memberID'];

$stmt = $conn->prepare("DELETE FROM member WHERE MemberID = ?");


$stmt->bind_param("i", $memberID);
$stmt->execute();
echo "Record deleted successfully<br/>";
}

// Select data
if (isset($_POST['select'])) {
$result = $conn->query("SELECT * FROM member");

if ($result->num_rows > 0) {
echo "<table border='1'><tr><th>ID</th><th>First Name</th><th>Last Name</
th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["MemberID"] . "</td><td>" .
htmlspecialchars($row["FirstName"]) . "</td><td>" . htmlspecialchars($row["LastName"]) .
"</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
}
$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Lab 6. Task 2</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 40px;
}

h2 {
color: #333;
}

form {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin-bottom: 20px;
}

input[type="text"], input[type="number"] {
width: calc(100% - 22px);
padding: 10px;
margin-top: 8px;
margin-bottom: 16px;
border: 1px solid #ddd;
border-radius: 4px;
}

input[type="submit"] {
background-color: #5cb85c;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}

input[type="submit"]:hover {
background-color: #4cae4c;
}

table {
width: 100%;
margin-top: 20px;
border-collapse: collapse;
}

table, th, td {
border: 1px solid #ccc;
}

th, td {
text-align: left;
padding: 8px;
}

th {
background-color: #eee;
}
</style>
</head>
<body>
<h2>Insert Member</h2>
<form method="post">
First Name: <input type="text" name="firstName" required><br>
Last Name: <input type="text" name="lastName" required><br>
<input type="submit" name="insert" value="Insert Data">
</form>

<h2>Update Member</h2>
<form method="post">
Member ID: <input type="number" name="memberID" required><br>
New First Name: <input type="text" name="firstName" required><br>
<input type="submit" name="update" value="Update Data">
</form>
<h2>Delete Member</h2>
<form method="post">
Member ID: <input type="number" name="memberID" required><br>
<input type="submit" name="delete" value="Delete Data">
</form>

<h2>Select Members</h2>
<form method="post">
<input type="submit" name="select" value="Show Members">
</form>
</body>
</html>

Output:
Task 3: Perform Task 6.2 using PHP-PDO (PHP Data Objects) interface.

Code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "task_4_5";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $user-
name, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully<br/>";

// Insert data
if (isset($_POST['insert'])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$stmt = $conn->prepare("INSERT INTO member (FirstName, LastName) VALUES
(:firstName, :lastName)");
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->execute();
echo "New record created successfully<br/>";
}

// Update data
if (isset($_POST['update'])) {
$memberID = $_POST['memberID'];
$firstName = $_POST['firstName'];
$stmt = $conn->prepare("UPDATE member SET FirstName = :firstName WHERE
MemberID = :memberID");
$stmt->bindParam(':memberID', $memberID);
$stmt->bindParam(':firstName', $firstName);
$stmt->execute();
echo "Record updated successfully<br/>";
}

// Delete data
if (isset($_POST['delete'])) {
$memberID = $_POST['memberID'];
$stmt = $conn->prepare("DELETE FROM member WHERE MemberID = :mem-
berID");
$stmt->bindParam(':memberID', $memberID);
$stmt->execute();
echo "Record deleted successfully<br/>";
}

// Select data
if (isset($_POST['select'])) {
$stmt = $conn->prepare("SELECT * FROM member");
$stmt->execute();
$result = $stmt->fetchAll();
if (count($result) > 0) {
echo "<table border='1'><tr><th>ID</th><th>First Name</th><th>Last Name</
th></tr>";
foreach ($result as $row) {
echo "<tr><td>" . htmlspecialchars($row["MemberID"]) . "</td><td>" . htmlspe-
cialchars($row["FirstName"]) . "</td><td>" . htmlspecialchars($row["LastName"]) . "</
td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
}
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>

<!DOCTYPE html>
<html>
<head>
<title>DBMS Lab Project - PDO Interface</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 40px;
}

h2 {
color: #333;
}

form {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}

input[type="text"],
input[type="number"] {
width: calc(100% - 22px);
padding: 10px;
margin-top: 8px;
margin-bottom: 16px;
border: 1px solid #ddd;
border-radius: 4px;
}

input[type="submit"] {
background-color: #5cb85c;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}

input[type="submit"]:hover {
background-color: #4cae4c;
}

table {
width: 100%;
margin-top: 20px;
border-collapse: collapse;
}

table,
th,
td {
border: 1px solid #ccc;
}

th,
td {
text-align: left;
padding: 8px;
}

th {
background-color: #eee;
}
</style>
</head>

<body>
<h2>Insert Member</h2>
<form method="post">
First Name: <input type="text" name="firstName" required><br>
Last Name: <input type="text" name="lastName" required><br>
<input type="submit" name="insert" value="Insert Data">
</form>

<h2>Update Member</h2>
<form method="post">
Member ID: <input type="number" name="memberID" required><br>
New First Name: <input type="text" name="firstName" required><br>
<input type="submit" name="update" value="Update Data">
</form>

<h2>Delete Member</h2>
<form method="post">
Member ID: <input type="number" name="memberID" required><br>
<input type="submit" name="delete" value="Delete Data">
</form>

<h2>Select Members</h2>
<form method="post">
<input type="submit" name="select" value="Show Members">
</form>
</body>
</html>

Output:
It has the same outputs as of task 2.

You might also like