0% found this document useful (0 votes)
38 views5 pages

Lab 7 PDF

DBMS LAB Ques

Uploaded by

paulpial09
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)
38 views5 pages

Lab 7 PDF

DBMS LAB Ques

Uploaded by

paulpial09
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/ 5

Department of Computer Science & Engineering

Course: Database Management Systems Lab (CSE331)


Credit Hour:1 Semester: Summer 2024

Lab 07
Database Connection Basics
I. Topic Overview:
In this lab, students will learn how to connect to MySQL database using PHP. Connecting
database to MySQL is an important step for the CSE331/ CCR 401 course project. Students
will learn different techniques (mostly involving mysql and mysqli and the fundamental
differences between $GET and $POST method. Students will also learn about the PHP
Data Objects (PDO).

II. Lesson Fit:


Students should have an understanding of the following:
1. SQL syntax
2. Any working PHP server (e.g. Xampp, Wampp, MySQLite etc.)
3. Basic HTML syntax

III. Learning Outcome:


After this exam, the students will:
a. Learn to create a basic form using HTML and PHP
b. Learn to connect their own databases to the form they’ve created.
c. Learn to use PDO instead of mysql and mysqli.
IV. Anticipated Challenges and Possible Solutions
Students might face problems in understanding link multiple *.php files.
Solutions: Teachers will explain the PHP directory and how to create different PHP web
pages.
V. Acceptance and Evaluation
Students are expected to complete all tasks during the class. This lab will be graded. Home
assignment will be given to the students who will fail to finish the given task within the
scheduled time.
VI. Activity Detail
Hour: 1
Students will be explained the basics of mysql and mysqli functions and the basics on how
to create a simple webpage with form and extract information from the user. Then they will
learn to connect the webpage to a database they’ve created previously.
Hour: 2
Students will get 1 hour to complete Task 1, 2 and 3. Students will be explained how to
work with PHPmyAdmin.
Homework
Students will complete Task 4, Task 5 and 6.
Lab 6 Activity List
Page 1 of 5
Department of Computer Science & Engineering
Course: Database Management Systems Lab (CSE331)
Credit Hour:1 Semester: Summer 2024

Suggestions for this Lab:

 Use a Text editor such as NotePad++ to open *.php and *.html files.
 Copy and Paste your program from the Text editor to the command line. If the program works,
save the program. Otherwise, fix the error and save it.
 Save your text file regularly.

Task 1
Create a simple new “form.php” file and design a simple text form with text boxes. Write the
following code:

<html>
<body>
<h1>A small example page to insert some data in to the MySQL database using
PHP</h1>
<form action="insert.php" method="post">
Firstname: <input type="text" name="fname" /><br><br>
Lastname: <input type="text" name="lname" /><br><br>

<input type="submit" />


</form>
</body>
</html>

Your form will look like this:

Task 2
a. Add another textbox for ID in Task 1.
Page 2 of 5
Department of Computer Science & Engineering
Course: Database Management Systems Lab (CSE331)
Credit Hour:1 Semester: Summer 2024

b. Change Firstname to Name


c. Change Lastname to CGPA. Change the input type to number. Specify max and min.
(https://www.w3schools.com/tags/att_input_type_number.asp )

Task 3
Create the “insert.php” file. Now use one approach below to connect to MySQL:

MySQLi Object-Oriented
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname= "dbname";

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

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
MySQLi Procedural
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname= "dbname";

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

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

PHP Data Object (PDO)

Page 3 of 5
Department of Computer Science & Engineering
Course: Database Management Systems Lab (CSE331)
Credit Hour:1 Semester: Summer 2024

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname= "dbname";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname ", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

Task 4

Open PhPmyAdmin and create database. In the database you should have a Students table with
the following attributes: ID, Name, CGPA

Task 5
a. Write the code to insert data manually into the database in the “insert.php” file
$sql = "INSERT INTO STUDENTS(ID, Name, CGPA)
VALUES ('01902192', 'Harry', '3.8')";
b. Make the necessary changes in insert.php according to
https://www.w3schools.com/php/php_mysql_insert.asp

Task 6
Now change your code to insert values from the form into the database. The data in each text
box/input button can be accessed using $_POST[fname].
E.g. $sql="INSERT INTO nametable (fname, lname) VALUES
('$_POST[fname]','$_POST[lname]')";

Examples
For selecting data from database: https://www.w3schools.com/php/php_mysql_select.asp
Page 4 of 5
Department of Computer Science & Engineering
Course: Database Management Systems Lab (CSE331)
Credit Hour:1 Semester: Summer 2024

For deleting data from database: https://www.w3schools.com/php/php_mysql_delete.asp


For updating data in database: https://www.w3schools.com/php/php_mysql_update.asp

Page 5 of 5

You might also like