Transcript cse103day21

CSE 103
Makeups
– If you didn’t take one over the weekend, take
one TUESDAY or WEDNESDAY!
– Next chance: take one between FRIDAY and
TUESDAY
But don’t take one during the game(s); that would
be lousy
– In-class BT: next THURSDAY (or MONDAY,
April 11)
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 1
Dynamically Generated Web
Pages
Dynamic web pages are generated as
needed or as requested
Allows you to make a huge number of
pages from a single file using a “scripting
language” such as PHP
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 2
Serving PHP files that use a
database
requests http://cse103.egr.msu.edu/user/sparty/actor.php
Client
Web
Server
Database
Server
The web server requests data from the database
server based on the PHP script
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 3
PHP and databases
PHP connects to MySQL
– Other database servers also supported
Specify which database to connect to
– Today we'll use the movies database
Store queries in the PHP file
– Web server sends them to database server
– Database server sends results back
Today we will start making a small “cast
browser” set of pages
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 4
CSE 103 functions
To simplify using PHP, we provide some common
utilities for you
–
–
–
–
Can use require() just like include() in order to use a library
Functions in that file can be called from the requiring file
They save you time; not having to learn picky details about PHP
Feel free to examine the source code (Day 21 AFS)
Start a new PHP page named actor.php in
Dreamweaver. (Save in your web folder!)
Include the function definitions from the file above
by entering at the top of your new page (line 1)
<?php
require('/afs/msu/course/cse/103/php/cse103.php');?>
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 5
Test database connection
Use set_database() to choose the database with
–
–
–
–
Server: "cse103.egr.msu.edu"
Database: "movies"
Username: "yourmsuNetID"
Password: "yourPID", with starting A capitalized
Add the PHP function get_SQLversion() to the
page in a print statement in the <body>
– This function finds the version of the MySQL database
server being used (4.1.8a-Debian_1-log)
Test the page by browsing to it:
http://cse103.egr.msu.edu/user/SPARTY/actor.php
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 6
Variables in PHP
Variables and constants let you store
values for later use
– The “value” of variables can change
All PHP variables begin with a $ followed
by a non-numeric character
– Example: $name = 'Bob';
– Assigns the value Bob to the variable $name
Only alphabetic, numeric, and _
characters are allowed in variable names
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 7
Query strings
An SQL query is usually several lines long
It is usually entered as a single string–in "quotes"
It is usually assigned to a variable, so that it can
easily be passed to functions
The query itself should NOT end with ; but the
PHP statement MUST end with one (OUTSIDE
the quotes around the query)
Example:
$query = "SELECT MovieTitle, Year
FROM tbl_Movies
WHERE Year = 2004";
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 8
Using a query to generate a
dynamic page
Create an SQL query that finds FirstName and
LastName of a person identified by ActorID
– Use ActorID=201101 to test the query
Store this (and every query you ever use) in a
variable
Use print and the text_from_query() function to
display the actor’s name
– Check the Day 21 classwork page for
documentation link
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 9
Using a query to generate a
dynamic table
Create a second SQL query that finds each
movie, year, and role in which s/he has
appeared – ordered by year
– Continue to restrict by ID number
Make a table on your page of the results of this
query using print and table_from_query()
– Check the support function documentation
linked from the Day 21 page if you need to
– Very similar to using text_from_query()
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 10
Movie information
Create a new PHP page called movie.php
Display name & year of movie (given MovieID)
as a heading on the page (in <h1> tags)
– Test with MovieID = 130990
Make a table displaying each credit number*,
role in the movie, and name of actor playing it
– Order by CreditNumber (*this field is in tbl_Roles)
– Use the MySQL concat() function to put both first
and last name of the actor into a single field
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 11
Passing information to a
dynamic page
Parameters can be passed in a URL
Example:
– …/actor.php?ActorID=201101
Same syntax can be used by forms in a
GET operation
– We will look at this next time
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 12
Using parameters in PHP
Each parameter becomes a special PHP variable,
of the form $_GET['key']=value, where key is the
name before = and value is after:
– …/actor.php?ActorID=1234
makes $_GET['ActorID']=1234
– Key is case-sensitive: $_GET['actorid']=0
This value can be used later in PHP
If you need to use it inside a quoted string, like a
query, put curly braces { } around it:
$query =
"…WHERE ActorID={$_GET['ActorID']} …";
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 13
Exercise - Using parameters in
PHP
Modify the actor.php page to GET the value
of ActorID from a parameter
Change your queries to use the correct
$_GET['xyz'] variable
Test cases:
– ActorID=615264 is Sandra Bullock and
ActorID=165619 is Keanu Reeves
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 14
Exercise – Using parameters again
Now modify the movie.php page to GET
the value of MovieID from a parameter
Change your queries to use the correct
$_GET['xyz'] variable
Test cases:
– MovieID=309772 is Star Wars III and
MovieID=130634 is Robots
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 15
Homework
Check the link on today’s classwork
page
– Finish classwork
– Read rest of Day 20 textbook, covering
HTML forms
– Work on UBTs!
http://www.cse.msu.edu/~cse103
U:/msu/course/cse/103
Day 21, Slide 16