Lab-6 by Anees
Lab-6 by Anees
WITH MYSQL
LAB # 06
Spring 2025
CSE-403L
Database Management System Lab
Unsatisfac- Stu-
Exemplary Acceptable Developing
tory dent
Score
Dimension out of
10 8 6 4 10
Marks
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.
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.
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();
<?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);
}
// Insert data
if (isset($_POST['insert'])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
// Update data
if (isset($_POST['update'])) {
$memberID = $_POST['memberID'];
$firstName = $_POST['firstName'];
// Delete data
if (isset($_POST['delete'])) {
$memberID = $_POST['memberID'];
// 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.