Upload
jody-walton
View
221
Download
1
Embed Size (px)
Citation preview
Intro to LAMP ProgrammingIntro to LAMP Programming
Presented for
CIS Faculty at SAC
by
Dan Zollars
What is LAMP?What is LAMP?
LinuxApacheMySQLPHP
Number one HTTP server on the Internet
Most popular open-source database
Widely used, general purpose scripting language
Getting StartedGetting Started
Website resources – see http://cis.sac.accd.edu/~dzollars, then LAMP demo
Need only vi editor and browser – working directly on sol using programs already set up
Put programs in public_html under personal directory
TopicsTopics
Testing Apache & PHP
Integrating PHP and HTML
Targeting a PHP script from an HTML form
Retrieving information from MySQL databases
Accessing MySQL databases from PHP
Practice
Testing Apache & PHPTesting Apache & PHP
In browser: http://cis.sac.accd.edu/~yourname
Any document in public_html is available for Apache to serve to client (even if no link)
/home/yourname/public_html/sample.html <--> http://cis.sac.accd.edu/~yourname/sample.html
Using Minimal XHTML document
Testing PHP: Example 1
Integrating PHP & HTMLIntegrating PHP & HTML
<body><?php
$x = 1;for ($i = 0; $i < 10; $i++)
echo “<p>\$i = $i</p>\n”;// more php code
?></body>
Example 2
Targeting a PHP scriptTargeting a PHP script
<form action=”target.php” method=”post”><input type=”text” name=”field_name” /><input type=”submit” />
</form>
Now in target.php:$field_name = $_POST['field_name'];
Example 3
Practice - 1Practice - 1
Write the target script for example3.php
Practice - 1 (answers)Practice - 1 (answers)
<?php $lastname = $_POST['lastname']; echo "<p>The name you entered was: $lastname</p>\n";?>
Retrieving MySQL InformationRetrieving MySQL Information
SELECT {<column_list> | *}
FROM <table1> [, <table2> ...]
[WHERE <condition>]
[ORDER BY <order> [DESC] ]
[GROUP BY <group_condition>] ;
SELECT ClauseSELECT Clause
Use the SELECT clause to restrict which columns to display
SELECT firstname, lastname, email
SELECT qty, item_desc
SELECT *
FROM ClauseFROM Clause
Use the FROM clause to specify which table(s) to retrieve the data from
SELECT firstname, lastname, emailFROM customers;
SELECT *FROM orders;
WHERE ClauseWHERE Clause
Use the WHERE clause to restrict the number of rows to display
SELECT qty, item_descFROM itemsWHERE qty > 1;
SELECT *FROM ordersWHERE paid IS NULL;
JOINSJOINS
Several kinds
Common column
Can use either the FROM or WHERE clause
JOIN - WHEREJOIN - WHERE
Uses the WHERE clause to specify join condition
SELECT order_id, order_date, lastnameFROM orders, customersWHERE orders.cust_id = customers.cust_id;
SELECT qty, item_descFROM items, ordersWHERE items.order_id = orders.order_idAND items.order_id = 1002;
Practice - 2Practice - 2
Use the satlug database to find out the following:
Names and addresses of all the customers
How many orders for each customer (just list them
and count)?
List the unfinished orders (completed IS NULL)
List the orders that have been shipped but haven't
been paid for yet
How many carrots did Bugs Bunny order (join items
to orders where cust_id = 4)?
Practice - 2 (answers)Practice - 2 (answers)
SELECT firstname, lastname, address, city, state FROM customers;
SELECT * FROM orders;
SELECT * FROM orders WHERE completed IS NULL;
Practice - 2 (answers)Practice - 2 (answers)
SELECT * FROM ordersWHERE completed IS NOT NULLAND paid IS NULL;
SELECT qty, item_descFROM items, ordersWHERE items.order_id = orders.order_idAND orders.cust_id = 4;
Accessing MySQL from PHPAccessing MySQL from PHP
$link = mysql_connect(“host”, “name”, “pw”);
mysql_select_db(“satlug”, $link);
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))echo “$row[0] $row[1] \n”; // etc.
die(“Error message” . mysql_error());
Example 4
Practice - 3Practice - 3
Modify example 3 source and target as follows:
Client enters last name, target displays first and last
names
Client enters cust_id, target displays order id and
order date for all orders
Client enters order_id, target displays qty and
description
Using $_GETUsing $_GET
In source file, create a link with parameter
<a href='target.php?id=$id'>Text</a>
In target file, use $_GET superglobal to get info
$id = $_GET['id'];
Creates different html for each table entry
Still only two files
Practice - 4Practice - 4
Modify practice 3 source and target as follows:
Source looks up customer names, presents as links to
target using HTTP parameter (display name, use id as
parameter)
Target uses $_GET to determine cust_id, then looks
up other customer information
Target displays information