0% found this document useful (0 votes)
21 views

L6 XAMPP PHP and Databases

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

L6 XAMPP PHP and Databases

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 60

CUIT 216:

WEB DESIGN AND E-BUSINESS

XAMPP PHP AND DATABASES


INTRODUCTION TO PHP
WEB DATABASE TRANSACTION
 A typical web database transaction consists of the following stages:
1. A user’s web browser issues an HTTP request for a particular
web page.
 For example, using an HTML form, he might have requested a
search for all books at MikkeliOnlineProfessionalBooks.com written
by Leila Karjalainen.
 The search results page may be called results.php.
2. The web server receives the request for results.php, retrieves
the file, and passes it to the PHP engine for processing.
WEB DATABASE TRANSACTION
3. The PHP engine begins parsing the script.
 Inside the script is a command to connect to the database and
execute a query (perform the search for books).
 PHP opens a connection to the MySQL server and sends on the
appropriate query.
4. The MySQL server receives the database query,
processes it, and sends the results - a list of books - back to
the PHP engine.
5. The PHP engine finishes running the script, which usually
involves formatting the query results nicely in HTML.
It then returns the resulting HTML to the web server.
WEB DATABASE TRANSACTION
6. The web server passes the HTML back to the browser, where the user can
see the list of books she requested.
 The above described process is basically the same regardless of which
scripting engine or database server you use.
 Sometimes the web server, PHP engine, and database server all run on
the same machine.
 However, it is quite common for the database server to run on a different
machine.
 You might do this for reasons of security, increased capacity, or load
spreading.
 From a development perspective, this approach is much the same to work
with.
WEB DATABASE TRANSACTION
1

Browse 6
Web
r Server

5 2

MySQL 4
PHP
Server Engine
WHAT IS PHP?

PHP == ‘Hypertext Preprocessor’


Open-source, SERVER-SIDE scripting language
PHP scripts reside between reserved PHP tags
The option is available to embed PHP scripts within HTML
pages
Executed on the server-side
WHAT IS PHP?
Source-code not visible by client
o ‘View Source’ in browsers does not display the PHP
code
Supports procedural and object-oriented paradigm
(to some degree)
All PHP statements end with a semi-colon
Each PHP script must be enclosed in the reserved
PHP tag
WHAT DOES PHP CODE LOOK LIKE?
Structurally similar to C/C++
Each PHP script must be enclosed in the reserved PHP tag
VARIABLES IN PHP
PHP variables must begin with a “$” sign
Case-sensitive ($Foo != $foo != $fOo)
Global and locally-scoped variables
 Global variables can be used anywhere
 Local variables restricted to a function or class
Certain variable names reserved by PHP
 Form variables ($_POST, $_GET)
 Server variables ($_SERVER)

VARIABLE USAGE
ECHO
The PHP command ‘echo’ is used to output the
parameters passed to it, typically to send data to the
client’s web-browser
ECHO EXAMPLE
ECHO EXAMPLE

Notice how echo ‘5x5=$foo’ outputs $foo rather than


replacing it with 25
Strings in single quotes (‘ ’) are not interpreted or
evaluated by PHP
This is true for both variables and character escape-
sequences (such as “\n” or “\\”)
PRINT

Echo has no return value while print has a return value


of 1 so it can be used in expressions.
echo can take multiple parameters (although such
usage is rare) while print can take one argument.
The echo statement can be used with or without
parentheses: echo or echo().
ECHO WITH HTML

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with
multiple parameters.";
?>
EXAMPLE
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2<br>";
echo $x + $y;
?>
EXAMPLE - PRINT

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
EXAMPLE - PRINT
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
XAMPP
Install XAMPP to the root directory: C:\XAMPP
Launch XAMPP control panel and start the
following services:
 MySql
 Apache
Open your web browser and enter in the URL box:
http://localhost
RUNNING XAMPP ERROR: OBJECT NOT FOUND
Go to XAMPP Control Panel
Click on config (APACHE)
Select Apache(httpd.conf)
Search for “DocumentRoot”
Look at the path pointing to the htdocs folder.
e.g "C:/xampp/htdocs“
Or "C:/Users/xampp/htdocs"
YOUR FIRST PHP APPLICATION

Navigate to “C:\XAMPP\htdocs”
Create a project folder called HelloPHP
Inside the folder create a file with a .php
extension called Hello World.
YOUR FIRST PHP APPLICATION

Write the
following code
into the file:
YOUR FIRST PHP APPLICATION

Go to the browser


In the URL box type “localhost”
From the folder list select the project folder
created above and click on the Hello World
file created before.
ARITHMETIC OPERATIONS
$a - $b // subtraction
$a * $b // multiplication
$a / $b // division
$a += 5 // $a = $a+5 Also works for *= and /=
CONCATENATION

Use a period to join strings into one.


ESCAPING THE CHARACTER
If the string has a set of double quotation marks that
must remain visible, use the \ [backslash] before the
quotation marks to ignore and display them.
Run the php script below from the browser as localhost.
PHP CONTROL STRUCTURES

 Control Structures: Are the structures within a


language that allow us to control the flow of
execution through a program or script.
 Grouped into conditional (branching) structures
(e.g. if/else) and repetition structures (e.g. while
loops).
EXAMPLE IF/ELSE IF/ELSE STATEMENT:
if ($foo == 0) {
echo ‘The variable foo is equal to 0’;
}
else if (($foo > 0) && ($foo <= 5)) {
echo ‘The variable foo is between 1 and 5’;
}
else {
echo ‘The variable foo is equal to ‘.$foo;
}
IF ... ELSE...
If (condition)
{
Statements;
}
Else
{
Statement;
}
WHILE LOOPS

While (condition)
{
Statements;
}
DATE DISPLAY

or
MONTH, DAY & DATE FORMAT SYMBOLS
FUNCTIONS

Functions MUST be defined before they can be


called
Function headers are of the format
function functionName($arg_1, $arg_2, …,
$arg_n)
Unlike variables, function names are not case
sensitive (foo(…) == Foo(…) == FoO(…))
FUNCTIONS EXAMPLE
WORKING WITH FORMS

Index.php
WORKING WITH FORMS

• php2.php
EXERCISE 1
Using a while loop, draw the following
pattern onto the browser using a PHP
script.
WORKING WITH DATABASES
FROM THE SCREEN:
 Click on phpMyadmin
 Click on Databases
 Enter db name (myDB) and click on create
 Click on the database name(to the left) and enter a table name
Students, specify 3 columns for it and click on Go.
 Enter 3 column names (name, Surname and RegNo) of type
VarChar and length 20 and click on Save
 Click on SQL,delete the query that’s appearing and enter the
following query:

INSERT INTO ‘Students’(‘name’,’Surname’, ‘RegNo’) VALUES


("Tom","Scott","W101010")

Click on Go.
1. VIEWING DATABASE CONTENT IN BROWSER
BROWSER

Create a new notepad document,


TableStudents.php and save it in C:\XAMPP\
htdocs
Open the document and save the following
information
BROWSER
BROWSER
VIEWING DB FROM BROWSER

Go to browser
Type in the URL Box:
localhost/TableStudents.php
The following page should appear in browser:
VIEWING DB FROM BROWSER
2. INSERTING VALUES INTO DATABASE FROM
BROWSER
INSERTING VALUES INTO DB FROM BROWSER

Command Syntax:

INSERT INTO table_name ( Columns ) VALUES


( values for columns)
DBINSERT.HTML
INSERT.PHP
CREATE AND RUN THE FILE
TABLESTUDENTS.PHP FROM THE BROWSER.
SEARCHING FROM DATABASE

Question:

Write the PHP code to search from the database


while viewing the results in a table?
EDITING INFORMATION IN A TABLE

Question:

Design a form and write the code that would enable


you to edit and update information in your
database table?
A LOG-IN PAGE - HOME.PHP
LOGIN.PHP
AFTERLOGIN.PHP
PRACTICE QUESTIONS
QUESTION
a) Create the interface shown below and use it to insert some data
into a database and to retrieve & view the data in a table:
QUESTION

b) Edit the form on the previous page so that the user can
click on a button labelled Edit.
When this button is clicked, a modal appears containing
textboxes that allows the user to edit the information in
the database.

You might also like