Lab 5 - PHP - MYSQL CS314
Lab 5 - PHP - MYSQL CS314
Date: / /
1. Objectives
2. Introduction
The labs for CS-314 provide time to do computer science under the direction of an
instructor who will be serving as a facilitator during the lab session. This means that the
instructor will not solve your problems or show you how to complete the activities. The
instructor may provide helpful hints, ask additional questions, ask you to explain a
solution, ask how you obtained a solution, or facilitate discussions about observations,
experiments, results, discoveries, and other topics that arise.
Some lab activities will ask you to develop solutions to problems, program solutions in
Web Application Development, and test the programs. Other activities will involve using
programs we give you to illustrate important concepts in Computer Science. You will be
required to make observations, form and test hypotheses, and use the results of your
testing to change aspects of your programs.
PHP Syntax Example (PHP Syntax, Comments, Variables, Echo/print, Data types, Strings, Numbers,
Constants, Operators, if...Else...Elseif, Switch, Loops, Functions, Arrays. Superglobals.)
PHP Forms - (PHP Form Handling, PHP Form Validation, PHP Form Required, PHP Form URL/E-
mail, PHP Form Complete.)
PHP MySQL Database - (MySQL Connect, MySQL Create DB, MySQL Create Table, MySQL
Insert Data, MySQL Prepared, MySQL Select Data, MySQL Where, MySQL Order By, MySQL Delete
Data, MySQL Update Data, MySQL Limit Data.)
CS 314 –
Web Application Development Lab Script
Date: / /
MySQL Database
To connect to database through PHP execute the following code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username,
$password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
The output of the above code should be: “Connected successfully”
Now go to your localhost and type phpMyAdmin
http://localhost/phpmyadmin/
Create a Database
CS 314 –
Web Application Development Lab Script
Date: / /
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
Output
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Date: / /
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
)";
$conn->close();
?>
Output
// Create connection
CS 314 –
Web Application Development Lab Script
Date: / /
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
Output
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Date: / /
$sql .= "INSERT INTO StudentSchedule (firstname, lastname, email)
VALUES ('Rami', 'Dooley', '[email protected]')";
$conn->close();
?>
Output
MySQL Prepared
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Date: / /
$firstname = "Novak";
$lastname = "Moe";
$email = "[email protected]";
$stmt->execute();
$firstname = "Rami";
$lastname = "Dooley";
$email = "[email protected]";
$stmt->execute();
$stmt->close();
$conn->close();
?>
Output
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
CS 314 –
Web Application Development Lab Script
Date: / /
echo "0 results";
}
$conn->close();
?>
Output
MySQL Where
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Output
MySQL Order By
CS 314 –
Web Application Development Lab Script
Date: / /
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
Output / Results
Date: / /
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
Output
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Date: / /
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Output
Your Task:
The goal of this task is to demonstrate knowledge of php + mySQL through
1. Create a new folder within webserver root i.e. htdocs. The name of the folder
should be your student number_t5.
2. Create PHP User Registration Form (Sign up) with MySQL Database:
a. PHP code. Example (index.php, signup.php, login.php, etc.….)
b. CSS. Style.css (stylesheet)
c. Database table: (employees, students, memberships, etc.…)
3. Your code should include:
a. Getting user information via a HTML form.
b. Validating user submitted information on form submit.
c. Database handling to save registered user to the database after
validation.
Assessment
1. Each student will show all the above parts running as demo to the Lab Instructor before
leaving the lab. Total marks for the lab is as follows
Marks (demo +
Part (demo + report)
report)
1 30
2 10
3 60
Total 100
CS 314 –
Web Application Development Lab Script
Date: / /
2. Students will prepare a report in which they will submit the snapshots taken while they
worked on each part. They will explain the figures to make sure that they understood
what they did.
Note: Any submission crossing the deadline will be penalized by 10% reduction per day.