WEB TECHNOLOGIES - ITC 351 Lecture - 9X

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

19-May-22

ITC351
Web Technologies
Lecture Nine
Introduction to PHP

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Silence Hour
Visit
https://www.w3schools.com/php
/php_mysql_connect.asp
Read for 25 minutes.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

1
19-May-22

Today’s Lecture
2.0
1.0
Perform
Connecting
CRUD
to MySQL
operations on
Database
MySQL
using PHP
Databases
using PHP

3.0 4.0
Design Combine PHP
HTML forms and HTML in a
for file single
uploads document
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Objectives of the Lecture


• By the end of the lecture, the student will
be able to:
– Connect to MySQL database and select a
database using PHP scripts.
– Use mysqli functions to process SQL
statements in PHP.
– Write PHP codes to accept and process files
uploaded by users.
– Combine PHP and HTML effectively in a single
document.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

2
19-May-22

Connecting to MySQL Database


Server from PHP
Currently, connecting to MySQL database is
achieved by using either mysqli or PDO
functions.
In this lecture we focus on mysqli
functions.
The connection parameter to the database
serves as a “bridge” created at the business
logic layer, between the presentation layer
(UI) and the data layer in a typical 3-tier
web application.
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Parameters for Connecting to MySQL Database


Server from PHP
 Establishing a connection to MySQL database in
PHP needs the following parameters:
$dbHost --- Is the name or the IP address of
the database server.
$dbUser --- Is an authorized username that
has the privilege to connect to the database
server.
$dbPassword --- Is the password
corresponding to the specified username.
$dbName --- The name of the preferred
database on the database server
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

3
19-May-22

Connecting to MySQL Database Server and


Selecting a Database Example
<?php
// Database connection parameters
$dbHost = 'localhost';
$dbUser = 'root';
$dbPassword = ‘mydbpassword';
$dbName = 'petstore';

// Creating the connection


$dbConn = new mysqli($dbHost,
$dbUser, $dbPassword, $dbName);
// Checking the connection
if ($dbConn->connect_error) {
die("Connection failed: " . $dbConn->connect_error);
}
echo "Connected successfully";
?>
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Closing the Connection to MySQL


Database Server.
The connection opened to the database in
a PHP script will be closed as soon as the
execution of the script ends.
It is however, advisable to do so explicitly
with the syntax; $dbConn->close();
Where $dbConn is the instance of an
opened connection (the “bridge”) to
MySQL database server.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

4
19-May-22

Executing Embedded SQL Statements in


PHP
• To execute embedded SQL statements on MySQL database
using PHP we go by the syntax below:
$queryResult = $dbConn ->query($sql_statement);
• Where $sql_statement is the SQL Statement you intend to
execute, and $queryResult is a variable or resource that
holds the results of the SQL statement you executed.
<?php
if ($dbConn->query($sql_statement) === TRUE) {
echo “Message upon successful execution of SQL
Statement";
} else {
echo “Error message: " . $dbConn->error;
}
$dbConn->close();
?> Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

The query( ) and fetch_object( ) Functions


• The fetch_object( ) fuction takes the result set returned
by the query( ) function as its parameter. E.g: $resultSet-
>fetch_object();
• In order to display the contents of the result set to users,
developers mostly use the while loop and create their
own record set to which they assign the contents of the
result set for onward display.
$resultSet = $dbConn->query(“SELECT * FROM $dbTable”);
while($recordSet = $resultSet->fetch_object()){
echo $recordSet->fieldname_1;
echo $recordSet->fieldname_2;
echo $recordSet->fieldname_3;
echo $recordSet->fieldname_n;
} Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

5
19-May-22

Creating Sample Tables using SQL


$sql_db = CREATE DATABASE IF NOT EXISTS staffDB;

$sql1 = “CREATE TABLE IF NOT EXISTS department(DeptID


VARCHAR(10), DeptName VARCHAR(25), Phone VARCHAR(21),
PRIMARY KEY(DeptID))”;

$sql2 = “CREATE TABLE IF NOT EXISTS staff(


StaffID VARCHAR(10), FullName VARCHAR(25),
Gender VARCHAR(6), Phone VARCHAR(21), Email VARCHAR(25),
DeptID VARCHAR(10),
PRIMARY KEY(StaffID),
CONSTRAINT staff_fk1 FOREIGN KEY(DeptID) REFERENCES REFERENCES
department(DeptID) ON UPDATE CASCADE ON DELETE NO ACTION)”;

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Executing a SELECT Statement


<?php
include(“dbConnFile.php”); // Including Database connection file.
$sql_staff =“SELECT * FROM staff”;
$selectResult = $dbConn->query($sql_staff);
while($staff_row = $selectResult->fetch_object())
{
echo “StaffID:”. $staff_row->StaffID;
echo “Staff Name: ”. $staff_row->FullName;
echo “Gender:”. $staff_row->Gender;
echo “Department:”. $staff_row->DeptID;
echo “ <br />”;
}
?> Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

6
19-May-22

File Uploads in PHP - 1


 With PHP, it is possible to upload files to the server.
 To allow users to upload files from a form can be very
useful.
 Example
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" id="file" /> <br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

File Uploads in PHP - 2


 Notice the following about the HTML form above:
 The enctype attribute of the <form> tag specifies
which content-type to use when submitting the form.
 "multipart/form-data" is used when a form requires
binary data, like the contents of a file, to be uploaded.
 The type="file" attribute of the <input /> tag
specifies that the input should be processed as a file.
 For example, when viewed in a browser, there will be a
Browse/Choose file button next to the input field.
 Note: Allowing users to upload files is a big security risk. Permit
only trusted users to perform file uploads to your
server, and remember to validate every input
including file uploads.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

7
19-May-22

A Sample File Upload Script in PHP


 The "upload_file.php" file contains the code for uploading a file:
<?php

if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb <br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}

?>

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Explanation of the File Upload Script


 The first parameter is the form's input name and the
second index can be either "name", "type", "size",
"tmp_name” or "error". Like this:
 $_FILES["file"]["name"] - the name of the uploaded file
 $_FILES["file"]["type"] - the type of the uploaded file
 $_FILES["file"]["size"] - the size in bytes of the uploaded
file
 $_FILES["file"]["tmp_name"] - the name of the temporary
copy of the file stored on the server.
 $_FILES["file"]["error"] - the error code resulting from the
file upload.
 For security reasons, you should add restrictions on what
the user is allowed to upload.
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

8
19-May-22

Combining PHP and HTML


 PHP and HTML can be combined to produce very
efficient dynamic web sites or applications.
 In the combination , the HTML document
structure is mostly declared once within the
normal <html> …</html> and <body>…
</body> tags.
 The PHP delimiters <?php… ?> can be used
within the page as many times as possible.
 There is no set order that PHP codes should go
into the HTML codes and vice versa, it all
depends on the logic the developer wishes to
implement.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Displaying the Results of SQL Queries in


HTML Tables.
<!DOCTYPE html>
<html>
<head> <title>PHP and HTML Combination!</title> </head>
<body bgcolor = “#D2C2C2”>
<?php
include(“includes/connection.php”); //Connection is included here from the includes folder.
$sql=“SELECT * FROM staff”;
$result = $dbConn->query($sql);
?>
<table border = “3” width = “45%” align = “center” style=“border-collapse:collapse;”>
<tr><td colspan = “4”>Our Cherished Staff - 2022</td> </tr>
<tr> <th>StaffID</th> <th>Full Name</th> <th>Gender</th> <th>Department</th> </tr>
<?php
while($staff_row = $result->fetch_object())
{ ?>
<tr>
<td><?php echo $staff_row->StaffID; ?></td>
<td><?php echo $staff_row->FullName; ?></td>
<td><?php echo $staff_row->Gender; ?></td>
<td><?php echo $staff_row->DeptID; ?></td>
</tr>
<?php } ?> Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.
</body> </html>

9
19-May-22

Exercise
• By using PHP and HTML, produce the 2 and
5 times table from 1 to 50 for your younger
siblings at the basic levels of education.

THE 2 TIMES TABLE THE 5 TIMES TABLE


2 X 1 = 2 5 X 1 = 5
2 X 2 = 4 5 X 2 = 10

2 X 50 = 100 5 X 50 = 250
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

End of Lectures for the Semester

APPRECIATIONS

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

10
19-May-22

I wish to express my SINCERE


GRATITUDE to you ALL for HAVING
COMPORTED YOURSELVES DURING
SUCH AN ENDURING SEMESTER.
I wish you the very best in all your
endeavors especially in your
upcoming end of semester exams
Appreciations

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Final Semester’s Quiz


• Your semester’s quiz comes off
this Thursday May 19, 2022
between 12:05 to 12:30 pm via
Google forms.
• The url will be communicated to
you via your reps. Stay active.
• Please prepare. It covers
everything discussed except PHP.
• Good Luck !!! Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

11
19-May-22

Examination
• Your End of Semester Exams is in two
Sections – Sections A and B.
• Section A comprises MCQs and Fill ins
for a total of 40 marks.
• In Section B you are required to
answer a question or two in your
answer booklet for a total of 20
marks. Do read instructions carefully.
• The exams covers all that we have
learnt. Good Luck!!!
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

12

You might also like