22
Intro to LAMP Programming Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

Embed Size (px)

Citation preview

Page 1: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

Intro to LAMP ProgrammingIntro to LAMP Programming

Presented for

CIS Faculty at SAC

by

Dan Zollars

Page 2: Intro 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

Page 3: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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

Page 4: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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

Page 5: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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

Page 6: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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

Page 7: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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

Page 8: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

Practice - 1Practice - 1

Write the target script for example3.php

Page 9: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

Practice - 1 (answers)Practice - 1 (answers)

<?php $lastname = $_POST['lastname']; echo "<p>The name you entered was: $lastname</p>\n";?>

Page 10: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

Retrieving MySQL InformationRetrieving MySQL Information

SELECT {<column_list> | *}

FROM <table1> [, <table2> ...]

[WHERE <condition>]

[ORDER BY <order> [DESC] ]

[GROUP BY <group_condition>] ;

Page 11: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

SELECT ClauseSELECT Clause

Use the SELECT clause to restrict which columns to display

SELECT firstname, lastname, email

SELECT qty, item_desc

SELECT *

Page 12: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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;

Page 13: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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;

Page 14: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

JOINSJOINS

Several kinds

Common column

Can use either the FROM or WHERE clause

Page 15: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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;

Page 16: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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)?

Page 17: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

Practice - 2 (answers)Practice - 2 (answers)

SELECT firstname, lastname, address, city, state FROM customers;

SELECT * FROM orders;

SELECT * FROM orders WHERE completed IS NULL;

Page 18: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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;

Page 19: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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

Page 20: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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

Page 21: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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

Page 22: Intro to LAMP Programming Presented for CIS Faculty at SAC by Dan Zollars

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