Lab 7 PDF
Lab 7 PDF
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).
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>
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
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";
?>
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
Page 5 of 5