intro to lamp programming presented for sat linux users' group by dan zollars
TRANSCRIPT
Intro to LAMP ProgrammingIntro to LAMP Programming
Presented forPresented for
SAT Linux Users' GroupSAT Linux Users' Group
byby
Dan ZollarsDan Zollars
What is LAMP?What is LAMP?
LLinuxinuxAApachepacheMMySQLySQLPPHPHP
Number one HTTP server on the InternetNumber one HTTP server on the Internet Most popular open-source databaseMost popular open-source database Widely used, general purpose scripting Widely used, general purpose scripting
languagelanguage
Getting StartedGetting Started
Website resources – see Website resources – see http://cis.sac.accd.edu/~dzollarshttp://cis.sac.accd.edu/~dzollars, then , then LAMP demoLAMP demo
Need text editor and browser – programs Need text editor and browser – programs are already set upare already set up
Root accessRoot access Apache document root: /var/www/htdocsApache document root: /var/www/htdocs Client – Server modelClient – Server model
TopicsTopics
Testing Apache & PHPTesting Apache & PHP Integrating PHP and HTMLIntegrating PHP and HTML Targeting a PHP script from an HTML formTargeting a PHP script from an HTML form Retrieving information from MySQL Retrieving information from MySQL
databasesdatabases Accessing MySQL databases from PHPAccessing MySQL databases from PHP PracticePractice
Testing Apache & PHPTesting Apache & PHP
In browser: In browser: http://localhosthttp://localhost Any document in document root is available Any document in document root is available
for Apache to serve to clientfor Apache to serve to client /var/www/htdocs/sample.html same as /var/www/htdocs/sample.html same as
http://localhost/sample.htmlhttp://localhost/sample.html Using Minimal XHTML documentUsing Minimal XHTML document Testing PHP: Example 1Testing PHP: Example 1
Integrating PHP & HTMLIntegrating PHP & HTML
<body><body><?php<?php
$x = 1;$x = 1;for ($i = 0; $i < 10; $i++)for ($i = 0; $i < 10; $i++)
echo “<p>\$i = $i</p>\n”;echo “<p>\$i = $i</p>\n”;// more php code// more php code
?>?></body></body>
Example 2Example 2
Targeting a PHP scriptTargeting a PHP script
<form action=”target.php” method=”post”><form action=”target.php” method=”post”><input type=”text” name=”field_name” /><input type=”text” name=”field_name” /><input type=”submit” /><input type=”submit” />
</form></form> Now in target.php:Now in target.php:
$field_name = $_POST['field_name'];$field_name = $_POST['field_name']; Example 3Example 3
Practice - 1Practice - 1
Write the target script for example3.phpWrite the target script for example3.php
Practice - 1 (answers)Practice - 1 (answers)
<?php<?php $lastname = $_POST['lastname']; $lastname = $_POST['lastname']; echo "<p>The name you entered was: echo "<p>The name you entered was: $lastname</p>\n";$lastname</p>\n";?>?>
Retrieving MySQL Retrieving MySQL InformationInformation
SELECT {<column_list> | *}SELECT {<column_list> | *}
FROM <table1> [, <table2> ...]FROM <table1> [, <table2> ...]
[WHERE <condition>][WHERE <condition>]
[ORDER BY <order> [DESC] ][ORDER BY <order> [DESC] ]
[GROUP BY <group_condition>] ;[GROUP BY <group_condition>] ;
SELECT ClauseSELECT Clause
Use the SELECT clause to restrict which Use the SELECT clause to restrict which columns to displaycolumns to display
SELECT firstname, lastname, emailSELECT firstname, lastname, email SELECT qty, item_descSELECT qty, item_desc SELECT *SELECT *
FROM ClauseFROM Clause
Use the FROM clause to specify which Use the FROM clause to specify which table(s) to retrieve the data fromtable(s) to retrieve the data from
SELECT firstname, lastname, emailSELECT firstname, lastname, emailFROM customers;FROM customers;
SELECT *SELECT *FROM orders;FROM orders;
WHERE ClauseWHERE Clause
Use the WHERE clause to restrict the Use the WHERE clause to restrict the number of rows to displaynumber of rows to display
SELECT qty, item_descSELECT qty, item_descFROM itemsFROM itemsWHERE qty > 1;WHERE qty > 1;
SELECT *SELECT *FROM ordersFROM ordersWHERE paid IS NULL;WHERE paid IS NULL;
JOINSJOINS
Several kindsSeveral kinds Common columnCommon column Can use either the FROM or WHERE Can use either the FROM or WHERE
clauseclause
JOIN - WHEREJOIN - WHERE Uses the WHERE clause to specify join Uses the WHERE clause to specify join
conditioncondition SELECT order_id, order_date, lastnameSELECT order_id, order_date, lastname
FROM orders, customersFROM orders, customersWHERE orders.cust_id = WHERE orders.cust_id = customers.cust_id;customers.cust_id;
SELECT qty, item_descSELECT qty, item_descFROM items, ordersFROM items, ordersWHERE items.order_id = orders.order_idWHERE items.order_id = orders.order_idAND items.order_id = 1002;AND items.order_id = 1002;
Miscellaneous MySQLMiscellaneous MySQL
mysql -pmysql -p show databases;show databases; use <database>;use <database>; show tables;show tables; describe <table>;describe <table>; grant all on testing.* to grant all on testing.* to fred@localhostfred@localhost
identified by “Yabba%Dabba&Do”;identified by “Yabba%Dabba&Do”; revoke <privilege> on testing.* from revoke <privilege> on testing.* from
user@”%”;user@”%”;
Security in MySQLSecurity in MySQL
Daemon/client architectureDaemon/client architectureRun daemon as mysql userRun daemon as mysql userRequire passwordsRequire passwords
USE mysql;USE mysql; SELECT host, user, passwordSELECT host, user, password
FROM user;FROM user; Set up non-root user for specific databasesSet up non-root user for specific databases
Practice - 2Practice - 2 Use the satlug database to find out the Use the satlug database to find out the
following:following: Names and addresses of all the customersNames and addresses of all the customers How many orders for each customer (just list How many orders for each customer (just list
them and count)?them and count)? List the unfinished orders (completed IS List the unfinished orders (completed IS
NULL)NULL) List the orders that have been shipped but List the orders that have been shipped but
haven't been paid for yethaven't been paid for yet How many carrots did Bugs Bunny order How many carrots did Bugs Bunny order
(join items to orders where cust_id = 4)?(join items to orders where cust_id = 4)?
Practice - 2 (answers)Practice - 2 (answers)
SELECT firstname, lastname, address, SELECT firstname, lastname, address, city, state FROM customers;city, state FROM customers;
SELECT * FROM orders;SELECT * FROM orders; SELECT * FROM orders SELECT * FROM orders
WHERE completed IS NULL;WHERE completed IS NULL;
Practice - 2 (answers)Practice - 2 (answers)
SELECT * FROM ordersSELECT * FROM ordersWHERE completed IS NOT NULLWHERE completed IS NOT NULLAND paid IS NULL;AND paid IS NULL;
SELECT qty, item_descSELECT qty, item_descFROM items, ordersFROM items, ordersWHERE items.order_id = orders.order_idWHERE items.order_id = orders.order_idAND orders.cust_id = 4;AND orders.cust_id = 4;
Accessing MySQL from PHPAccessing MySQL from PHP
$link = mysql_connect(“host”, “name”, $link = mysql_connect(“host”, “name”, “pw”);“pw”);
mysql_select_db(“satlug”, $link);mysql_select_db(“satlug”, $link); $result = mysql_query($query);$result = mysql_query($query); while ($row = mysql_fetch_array($result))while ($row = mysql_fetch_array($result))
echo “$row[0] $row[1] \n”;echo “$row[0] $row[1] \n”;// etc.// etc. die(“Error message” . mysql_error());die(“Error message” . mysql_error()); Example 4Example 4
Practice - 3Practice - 3
Modify example 3 source and target as Modify example 3 source and target as follows:follows:
Client enters last name, target displays first Client enters last name, target displays first and last namesand last names
Client enters cust_id, target displays order id Client enters cust_id, target displays order id and order date for all ordersand order date for all orders
Client enters order_id, target displays qty and Client enters order_id, target displays qty and descriptiondescription
Using $_GETUsing $_GET●In source file, create a link with parameterIn source file, create a link with parameter
● <a href='target.php?id=$id'>Text</a><a href='target.php?id=$id'>Text</a>●In target file, use $_GET superglobal to In target file, use $_GET superglobal to get infoget info
● $id = $_GET['id'];$id = $_GET['id'];●Creates different html for each table entryCreates different html for each table entry●Still only two filesStill only two files
Practice - 4Practice - 4
●Modify practice 3 source and target as Modify practice 3 source and target as follows:follows:
● Source looks up customer names, presents as Source looks up customer names, presents as
links to target using HTTP parameter (display links to target using HTTP parameter (display
name, use id as parameter)name, use id as parameter)
● Target uses $_GET to determine cust_id, then Target uses $_GET to determine cust_id, then
looks up other customer informationlooks up other customer information
● Target displays informationTarget displays information
PHP ReviewPHP Review
What's wrong with this: What's wrong with this: echo "<p>Zollars' real name is "$dufus"</p>";echo "<p>Zollars' real name is "$dufus"</p>";$query = “SELECT fname, lname “$query = “SELECT fname, lname “
. “FROM customers “. “FROM customers “
. “WHERE department = Admin “;. “WHERE department = Admin “;$query = “SELECT qty, desc “$query = “SELECT qty, desc “
. “FROM inventory “. “FROM inventory “
. “WHERE partno LIKE 'L%';. “WHERE partno LIKE 'L%';
PHP Odds and EndsPHP Odds and Ends
PHP Provides lots of useful things:PHP Provides lots of useful things: include() or include_once();include() or include_once();
include_once(“connect.php”);include_once(“connect.php”);include(“header.php”);include(“header.php”);
foreach ($a as $x)foreach ($a as $x)Associative arraysAssociative arrays
$state['TX'] = “Texas”;$state['TX'] = “Texas”;foreach ($state as $key=>$name)foreach ($state as $key=>$name)
echo “<p>$key is the state of $name</p>”echo “<p>$key is the state of $name</p>”