Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to LAMP Programming Presented for SAT Linux Users' Group by Dan Zollars.

Similar presentations


Presentation on theme: "Intro to LAMP Programming Presented for SAT Linux Users' Group by Dan Zollars."— Presentation transcript:

1 Intro to LAMP Programming Presented for SAT Linux Users' Group by Dan Zollars

2 What is LAMP? Linux Apache MySQL PHP Linux Apache MySQL PHP Number one HTTP server on the Internet Number one HTTP server on the Internet Most popular open-source database Most popular open-source database Widely used, general purpose scripting language Widely used, general purpose scripting language

3 Getting Started Website resources – see http://cis.sac.accd.edu/~dzollars, then LAMP demo Website resources – see http://cis.sac.accd.edu/~dzollars, then LAMP demo http://cis.sac.accd.edu/~dzollars Need text editor and browser – programs are already set up Need text editor and browser – programs are already set up Root access Root access Apache document root: /var/www/htdocs Apache document root: /var/www/htdocs Client – Server model Client – Server model

4 Topics Testing Apache & PHP Testing Apache & PHP Integrating PHP and HTML Integrating PHP and HTML Targeting a PHP script from an HTML form Targeting a PHP script from an HTML form Retrieving information from MySQL databases Retrieving information from MySQL databases Accessing MySQL databases from PHP Accessing MySQL databases from PHP Practice Practice

5 Testing Apache & PHP In browser: http://localhost In browser: http://localhosthttp://localhost Any document in document root is available for Apache to serve to client Any document in document root is available for Apache to serve to client /var/www/htdocs/sample.html same as http://localhost/sample.html /var/www/htdocs/sample.html same as http://localhost/sample.html http://localhost/sample.html Using Minimal XHTML document Using Minimal XHTML document Testing PHP: Example 1 Testing PHP: Example 1

6 Integrating PHP & HTML \$i = $i \n”; // more php code ?> \$i = $i \n”; // more php code ?> Example 2 Example 2

7 Targeting a PHP script Now in target.php: $field_name = $_POST['field_name']; Now in target.php: $field_name = $_POST['field_name']; Example 3 Example 3

8 Practice - 1 Write the target script for example3.php Write the target script for example3.php

9 Practice - 1 (answers) The name you entered was: $lastname \n"; ?> The name you entered was: $lastname \n"; ?>

10 Retrieving MySQL Information SELECT { | *} FROM [,...] [WHERE ] [ORDER BY [DESC] ] [GROUP BY ] ;

11 SELECT Clause Use the SELECT clause to restrict which columns to display Use the SELECT clause to restrict which columns to display SELECT firstname, lastname, email SELECT firstname, lastname, email SELECT qty, item_desc SELECT qty, item_desc SELECT * SELECT *

12 FROM Clause Use the FROM clause to specify which table(s) to retrieve the data from Use the FROM clause to specify which table(s) to retrieve the data from SELECT firstname, lastname, email FROM customers; SELECT firstname, lastname, email FROM customers; SELECT * FROM orders; SELECT * FROM orders;

13 WHERE Clause Use the WHERE clause to restrict the number of rows to display Use the WHERE clause to restrict the number of rows to display SELECT qty, item_desc FROM items WHERE qty > 1; SELECT qty, item_desc FROM items WHERE qty > 1; SELECT * FROM orders WHERE paid IS NULL; SELECT * FROM orders WHERE paid IS NULL;

14 JOINS Several kinds Several kinds Common column Common column Can use either the FROM or WHERE clause Can use either the FROM or WHERE clause

15 JOIN - WHERE Uses the WHERE clause to specify join condition Uses the WHERE clause to specify join condition SELECT order_id, order_date, lastname FROM orders, customers WHERE orders.cust_id = customers.cust_id; SELECT order_id, order_date, lastname FROM orders, customers WHERE orders.cust_id = customers.cust_id; SELECT qty, item_desc FROM items, orders WHERE items.order_id = orders.order_id AND items.order_id = 1002; SELECT qty, item_desc FROM items, orders WHERE items.order_id = orders.order_id AND items.order_id = 1002;

16 Miscellaneous MySQL mysql -p mysql -p show databases; show databases; use ; use ; show tables; show tables; describe ; describe ; grant all on testing.* to fred@localhost identified by “Yabba%Dabba&Do”; grant all on testing.* to fred@localhost identified by “Yabba%Dabba&Do”;fred@localhost revoke on testing.* from user@”%”; revoke on testing.* from user@”%”;

17 Security in MySQL Daemon/client architecture Daemon/client architecture Run daemon as mysql user Run daemon as mysql user Require passwords Require passwords USE mysql; USE mysql; SELECT host, user, password FROM user; SELECT host, user, password FROM user; Set up non-root user for specific databases Set up non-root user for specific databases

18 Practice - 2 Use the satlug database to find out the following: Use the satlug database to find out the following: Names and addresses of all the customers Names and addresses of all the customers How many orders for each customer (just list them and count)? How many orders for each customer (just list them and count)? List the unfinished orders (completed IS NULL) List the unfinished orders (completed IS NULL) List the orders that have been shipped but haven't been paid for yet 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)? How many carrots did Bugs Bunny order (join items to orders where cust_id = 4)?

19 Practice - 2 (answers) SELECT firstname, lastname, address, city, state FROM customers; SELECT firstname, lastname, address, city, state FROM customers; SELECT * FROM orders; SELECT * FROM orders; SELECT * FROM orders WHERE completed IS NULL; SELECT * FROM orders WHERE completed IS NULL;

20 Practice - 2 (answers) SELECT * FROM orders WHERE completed IS NOT NULL AND paid IS NULL; SELECT * FROM orders WHERE completed IS NOT NULL AND paid IS NULL; SELECT qty, item_desc FROM items, orders WHERE items.order_id = orders.order_id AND orders.cust_id = 4; SELECT qty, item_desc FROM items, orders WHERE items.order_id = orders.order_id AND orders.cust_id = 4;

21 Accessing MySQL from PHP $link = mysql_connect(“host”, “name”, “pw”); $link = mysql_connect(“host”, “name”, “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)) echo “$row[0] $row[1] \n”;// etc. while ($row = mysql_fetch_array($result)) echo “$row[0] $row[1] \n”;// etc. die(“Error message”. mysql_error()); die(“Error message”. mysql_error()); Example 4 Example 4

22 Practice - 3 Modify example 3 source and target as follows: Modify example 3 source and target as follows: Client enters last name, target displays first and last names 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 cust_id, target displays order id and order date for all orders Client enters order_id, target displays qty and description Client enters order_id, target displays qty and description

23 Using $_GET ● In source file, create a link with parameter ● Text ● Text ● In target file, use $_GET superglobal to get info ● $id = $_GET['id']; ● Creates different html for each table entry ● Still only two files

24 Practice - 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

25 PHP Review What's wrong with this: What's wrong with this: echo " Zollars' real name is "$dufus" "; echo " Zollars' real name is "$dufus" "; $query = “SELECT fname, lname “. “FROM customers “. “WHERE department = Admin “; $query = “SELECT fname, lname “. “FROM customers “. “WHERE department = Admin “; $query = “SELECT qty, desc “. “FROM inventory “. “WHERE partno LIKE 'L%'; $query = “SELECT qty, desc “. “FROM inventory “. “WHERE partno LIKE 'L%';

26 PHP 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 arrays Associative arrays $state['TX'] = “Texas”; $state['TX'] = “Texas”; foreach ($state as $key=>$name) echo “ $key is the state of $name ” foreach ($state as $key=>$name) echo “ $key is the state of $name ”


Download ppt "Intro to LAMP Programming Presented for SAT Linux Users' Group by Dan Zollars."

Similar presentations


Ads by Google