Title: PHP Overview
1PHP Overview
- PHP Hypertext Preprocessor
- Server-Side Scripting
- Code also included with HTML
- Pre-processed on server
- Browser gets only HTML and client-side code
- More secure than JavaScript
- More powerful than JavaScript
- File access, database access, etc.
- Less efficient than JavaScript for interactive
features - Must go back to the server
2PHP Overview
- Open source software
- Free, users can add features
- E.g., phpMyAdmin, DBMS connectivity
- Evolving documentation (www.php.net)
- Competitors
- Active Server Pages (Microsoft)
- Java servlets and JSP (Sun)
- Cold Fusion (Macromedia)
3PHP Overview
- Summary
- JavaScript is the sizzle, PHP is the substance
- Secure method for
- Processing form data
- More extensive calculations, manipulations
possible - Selective display of database data
- Updates, deletions from databases
- Separating content from design
- Keeping track of information from one web page to
another - E-mailing information
- Validating users (logins, privileges, etc.)
- Etc.
4PHP Overview
- Basic examples
- Output
- Use of built-in functions (and php.net)
- User-written functions
- Control structures
- Processing form input
- Processing querystring input
- POST and GET methods
- Session variables
5PHP Overview
Try it
- Two DB examples (quick look first)
- List all customer information from database
- Design
- Connect to the DB server
- Select the DB we want to use (CS19)
- Get the Customer information from the DB
- Display it using HTML
- Disconnect from the DB
- Note Compare to the steps we use to directly
connect to the DB using phpMyAdmin
6PHP Overview
Connect to the DB server and select the DB
CS19 lt?php include ("/home/ktreu/aux/common_db
.inc") linkID mysql_connect("localhost",dbu
sername,dbuserpassword) or die ("Could not
connect " . mysql_error()) mysql_select_db("C
S19", linkID) or die ("Unable to select db "
. mysql_error())
Notes php delimiter, variables, including
external files, error checking
7PHP Overview
Get the list of Customers from the DB SQL
"SELECT FROM Customers ORDER BY
name" allValues mysql_query(SQL,
linkID) if (!allValues) echo "Could
not successfully run query (SQL) from DB " .
mysql_error() exit
Notes use of SQL, possibility of multiple links
to DB server, built-in MySQL functions, error
checking, contents of allValues
8PHP Overview
echo "lth4gtAll Customers Ordered by Last
Namelt/h4gt" echo "ltTABLE BORDER1
CELLPADDING8gt" echo "ltTRgtltTDgtltBgtCustomer
Numberlt/Bgtlt/TDgtltTDgtltBgtNamelt/Bgtlt/TDgtltTDgtltBgtPhonelt/B
gtlt/TDgt" while (thisValue mysql_fetch_array(al
lValues)) echo "ltTRgt" echo "ltTDgt" .
thisValue"custnum" . "lt/TDgt" echo "ltTDgt" .
thisValue"name" . "lt/TDgt" echo "ltTDgt" .
thisValue"phone" . "lt/TDgt" echo
"lt/TRgt" echo "lt/TABLEgt" mysql_close(linkID)
?gt
Notes printing HTML, looping, accessing each
record from the query one by one, closing the
connection
9PHP Overview
- What changes would be needed to get different
data? - List all Product information?
- List all products sold in March with the name of
the customer who purchased them? - Etc.
10PHP Overview
Try it
- Another example (quick look)
- List all customer information from database for a
specific customer - Design
- Get the desired customer number from the user
- Connect to the DB server
- Select the DB we want to use (CS19)
- Get the information for the customer with the
desired customer number from the DB - Display it using HTML
- Disconnect from the DB
11PHP Overview
Write a form to get the customer number ltform
method"post" action"listCustomersByCustnum.php"gt
Please enter a Customer Number ltinput
type"text" name"custnum" length"4"
maxlength"4"gt ltbrgt ltinput type"submit"
value"Click for Customer Info"gt lt/formgt
Notes method and action attributes of the form
12PHP Overview
Only two changes necessary in the PHP
code custno _POST'custnum' SQL
"SELECT FROM Customers WHERE custnumcustno"
allValues mysql_query(SQL, linkID) if
(!allValues) echo "Could not
successfully run query (SQL) from DB " .
mysql_error() exit
Notes Accessing the form data, using it in a
query, significance of double quotes
13Additions
- Edit
- Delete
- Insert
- Admin functions
- Other tips and tricks