Slideshow transcript
Slide 1: Welcome to LAMP Workshop Linux, Apache, MySQL and PHP Hussain Fakhruddin hussulinux@gmail.com hussain@teks.co.in
Slide 2: GNUnify '07: LAMP Workshop: hussulinux@gmail.com Overview of LAMP Introduction Client Server Model, What are web servers? Configuration of Linux , Apache Server. What is PHP? Why Use PHP? PHP Crash Course Language Reference: Variables, Controls, Loops etc. What is MySQL? Overview MySQL Crash Course Exercise 2 Sunday 28th Jan '07
Slide 3: GNUnify '07: LAMP Workshop: hussulinux@gmail.com Introduction S i m p le C li e n t S e r v e r M o d e l N e tw o rk S e r ve r R eque st L o c a l fi l e D ocum ent S ystem B ro w se r 3 Sunday 28th Jan '07
Slide 4: GNUnify '07: LAMP Workshop: hussulinux@gmail.com What is PHP PHP: Hypertext Preprocessor Why not HTP or HP or HPP? Recursive Acronym An acronym in which the first letter of the first word is represented by the acronym itself. GNU = “GNU is NOT UNIX” PHP: PHP Hypertext Preprocessor 4 Sunday 28th Jan '07
Slide 5: GNUnify '07: LAMP Workshop: hussulinux@gmail.com Why Use PHP If you want to add dynamic content to your pages If you want to make your pages easier to maintain If you’re learning your first \"real\" computing language If you need a solution that’s portable across multiple platforms (e.g. Red Hat Linux to Windows 2000) If you like free software or need a free solution Examples of uses of PHP & MySQL: Sign-up Forms – Surveys – Polls – Email a Postcard – Content Management – 5 Sunday 28th Jan '07
Slide 6: GNUnify '07: LAMP Workshop: hussulinux@gmail.com Overview of PHP Open Source server-side scripting language designed specifically for the web. Can also be used for command-line scripting and writing client- side GUI applications. Conceived in 1994, now used on +10 million web sites. Outputs not only HTML but can output XML, images (JPG & PNG), PDF files and even Flash movies (using libswf and Ming) all generated on the fly. Can write these files to the filesystem. Supports a wide-range of databases (20 + ODBC). PHP also has support for talking to other services using protocols such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP. Perl- and C-like syntax. Relatively easy to learn. Website @ http://www.php.net/ 6 Sunday 28th Jan '07
Slide 7: GNUnify '07: LAMP Workshop: hussulinux@gmail.com What you need to start a website? What you need? Server – PHP parser – Configurations. – 7 Sunday 28th Jan '07
Slide 8: GNUnify '07: LAMP Workshop: hussulinux@gmail.com PHP Crash Course Embedding PHP in HTML Adding dynamic content How to find system information phpinfo() – Variables are preceded by '$' Accessing Form Variables by $_GET, $_POST Operators Control Structures 8 Sunday 28th Jan '07
Slide 9: GNUnify '07: LAMP Workshop: hussulinux@gmail.com PHP Crash Course (cont.) Embedding PHP in HTML <html> <body> <strong>Hello World!</strong><br /> <? echo ‘My name is Dave Olsen!’; ?> </body> </html> PHP tag styles: XML: <?php ?>, Short: <? ?>, ASP: <% %> Script: <script language=‘php’></script> 9 Sunday 28th Jan '07
Slide 10: GNUnify '07: LAMP Workshop: hussulinux@gmail.com PHP Crash Course (cont.) Adding dynamic content by adding the date to the page. <html> <body> <strong>Hello World!</strong><br /> <? echo ‘Today is’; echo date(‘H:i jS F’); ?> </body> </html> Date() http://www.php.net/manual/en/function.date.php PHP Function Reference http://www.php.net/manual/en/funcref.php One useful function is phpinfo(). Gives system information so you can quickly find out what’s on your machine. 10 Sunday 28th Jan '07
Slide 11: GNUnify '07: LAMP Workshop: hussulinux@gmail.com PHP Crash Course (cont.) Variables: Are the symbols we use to represent data. Variable names can be of any length; can include letters, numbers and underscores; cannot start with a digit; case-sensitive; and can have the same name as a function. To assign values to variables: $foo = ‘bar’; Data Type: String $foo = 1; Data Type: integer $foo = 5.34; Data Type: Double $foo = array(“bar”,”united”); Data Type: Array Data Types are automatically assigned though you can force a data type by type casting. For example: $foo = ‘Hello’; $bar = (int)$foo; $bar now equals 0 Almost all variables are local. Globals include $_POST 11 Sunday 28th Jan '07
Slide 12: GNUnify '07: LAMP Workshop: hussulinux@gmail.com PHP Crash Course (cont.) Accessing Form Variables Three methods Short: $varfoo, – Medium: $_POST[‘varfoo’], (recommended for versions of PHP +4.1) – Long: $HTTP_POST_VARS[‘varfoo’] – Need register_globals ON for short method to work. Not expected to be ON on the WVU server. I’m open to input on the issue. Tip: For checkbox variables your variable name should end with [ ] Checkbox results are automatically put into an array – Example: <input type=checkbox name=foo[] value=Y> – 12 Sunday 28th Jan '07
Slide 13: GNUnify '07: LAMP Workshop: hussulinux@gmail.com PHP Crash Course (cont.) Operators: Operators are symbols that you can use to manipulate values and variables by performing an operation on them. Web Site @ http://www.php.net/manual/en/language.operators.php Includes: Assignment (e.g. =, +=, *=) – Arithmetic (e.g. +, -, *) – Comparison (e.g. <, >, >=, ==) – Logical (e.g. !, &&, ||) – 13 Sunday 28th Jan '07
Slide 14: GNUnify '07: LAMP Workshop: hussulinux@gmail.com PHP Crash Course (cont.) Control Structures: Are the structures within a language that allow us to control the flow of execution through a program or script. Grouped into conditional (branching) structures (e.g. if/else) and repetition structures (e.g. while loops). Example if/elseif/else statement: if ($foo == 0) { echo ‘The variable foo is equal to 0’; } else if (($foo > 0) && ($foo <= 5)) { echo ‘The variable foo is between 1 and 5’; } else { echo ‘The variable foo is equal to ‘.$foo; } Good code will use indents and comments. 14 Sunday 28th Jan '07
Slide 15: GNUnify '07: LAMP Workshop: hussulinux@gmail.com Overview of MySQL Relational database management system (RDBMS) Free Website @ http://www.mysql.com/ 15 Sunday 28th Jan '07
Slide 16: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course Database Basics Common SQL Statements INSERT – SELECT – UPDATE – DELETE – Simple Join Entity-Relationship (ER) Modeling An Easy Way to Manage Your MySQL DBs 16 Sunday 28th Jan '07
Slide 17: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) Database Basics A relational database manager (MySQL) manages databases which holds tables which has records (rows) with attributes (columns) Each record must have a unique ID, also known as a Primary Key. When used as an identifier in another table it’s called a Foreign Key. Used for joins. Each attribute has to have a data type. (e.g. int, text, varchar) A database language (SQL) is used to create and delete databases and manage data 17 Sunday 28th Jan '07
Slide 18: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) Table structure for following examples: CREATE TABLE oscarpool ( CREATE TABLE bestdirector ( uid int(4) auto_increment, bdid int(4) auto_increment, username varchar(255), name varchar(255), email varchar(255), PRIMARY KEY (bdid) bestpicture int(2), ) PRIMARY KEY (uid) ) Created two tables, ‘oscarpool’ & ‘bestdirector’ Instead of using SQL to create tables use phpMyAdmin url: http://www.phpmyadmin.net/ 18 Sunday 28th Jan '07
Slide 19: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) Common SQL Statement: INSERT INSERT INTO oscarpool (username,email,bestpicture) VALUES (‘dolsen',‘dave@usablecode.com',1) Creates a new record in the table ‘oscarpool’ Text fields need to have ‘s. Tip: If you have an ‘ in your data you need to escape it before inserting it. Can use the PHP function addslashes(). Example: ‘John O\\’Brien’ 19 Sunday 28th Jan '07
Slide 20: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) Common SQL Statement: SELECT SELECT uid,username FROM oscarpool Selects the attributes ‘uid’ and ‘username’ from every record in ‘oscarpool’ SELECT is how you query the database. You can also: limit the number of records returned with LIMIT, – limit retrieval to those records that match a condition with WHERE, – sort the data after the query has been evaluated using ORDER BY – Tip: To easily select every attribute replace ‘uid’ with ‘*’ 20 Sunday 28th Jan '07
Slide 21: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) Common SQL Statement: UPDATE UPDATE oscarpool SET email = ‘david.olsen@mail.wvu.edu’ WHERE uid = 1 Updates the email address where ‘uid = 1’ in the table ‘oscarpool’ In this case I know that uid 1 is what my record was. In many cases you’d pass a uid variable from a form. 21 Sunday 28th Jan '07
Slide 22: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) Common SQL Statement: DELETE DELETE FROM oscarpool WHERE uid = 1 Deletes the record where ‘uid = 1’ in the table ‘oscarpool’ DELETE only removes the record from the table. To remove an entire table from the database you need to use the SQL statement DROP. Tip: To remove every record in a table but not remove the table just don’t include the WHERE clause. 22 Sunday 28th Jan '07
Slide 23: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) Simple Join SELECT bd.name FROM oscarpool op, bestdirector bd WHERE op.uid = 1 and op.bestdirector = bd.bdid Selects the name of the Best Director that the user with ‘uid = 1’ has chosen bestdirector is a Foreign Key of the Primary Key for the table BestDirector Tip: Try to not have fields from two different tables have the same name. Gets confusing when trying to output this data when we connect with PHP. 23 Sunday 28th Jan '07
Slide 24: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) Entity-Relationship (ER) Modeling ER Modeling is the simple and clear method of expressing the design (relations) of a database between tables and attributes. Rectangles – Represent entities. Diamonds – Represent relationships between entities Ellipses – Represent attributes that describe an entity Lines – Connect entities to relationships. Can have annotation. M = many, 1 = one. Lines – Connects entities to attributes. No annotation. Entity = Table, Attributes = Attributes Web Database Applications by O’Reilly Publishers gives decent overview 24 Sunday 28th Jan '07
Slide 25: GNUnify '07: LAMP Workshop: hussulinux@gmail.com MySQL Crash Course (cont.) An easy way to manage your DBs phpMyAdmin is a browser-based administration tool for MySQL. Needs to be installed on your server. It can: create and drop databases – create, copy, drop, rename and alter tables – delete, edit and add fields – manage keys on fields – load text files into tables – create (*) and read dumps of tables – and more – Download phpMyAdmin @ http://www.phpmyadmin.net/ Note: phpMyAdmin does not come with out-of-the-box security. You either need to edit the config files to authenticate from a table or use .htaccess. 25 Sunday 28th Jan '07
Slide 26: GNUnify '07: LAMP Workshop: hussulinux@gmail.com Using PHP to Query a MySQL Database <html> <body> <h1>A List of Users Who Have Signed Up For OscarPool</h1> <? $dbh = mysql_connect(\"localhost\",“dbusername\",“dbpassword\") or die(“Couldn't connect to database.\"); $db = mysql_select_db(“dbname\", $dbh) or die(“Couldn't select database.\"); $sql = “SELECT username, email FROM oscarpool”; $result = mysql_query($sql, $dbh) or die(“Something is wrong with your SQL statement.\"); while ($row = mysql_fetch_array($result)) { $username = $row[‘username’]; $email = $row[‘email’]; echo ‘<a href=“mailto:’.$email.’”>’.$username.’</a><br />\\n’; } ?> </body> </html> 26 Sunday 28th Jan '07
Slide 27: GNUnify '07: LAMP Workshop: hussulinux@gmail.com Using PHP to Query a MySQL Database (cont.) Notes for previous slide example: The first option in mysql_connect can be an IP address. – mysql_query returns a small table with your results in it. The while – loop then goes through each record of that small table and pulls out the attributes/fields you selected in your SQL statement. die( ) will kill the script. Make sure that that text is informative. – If you use a function in your SQL query then it has to be a part of the – $row statement. For example, UNIX_TIMESTAMP(datefield) would be $row[‘UNIX_TIMESTAMP(datefield)’] \\n stands for a new line so that your source code will look a little – neater: PHP MySQL functions @ – url: http://www.php.net/manual/en/ref.mysql.php 27 Sunday 28th Jan '07
Slide 28: GNUnify '07: LAMP Workshop: hussulinux@gmail.com Lets Code 28 Sunday 28th Jan '07
Slide 29: GNUnify '07: LAMP Workshop: hussulinux@gmail.com PHP & MySQL Resources Web Sites http://www.php.net/ – http://www.phpbuilder.com/ – http://www.devshed.com/ – http://www.phpmyadmin.net/ – http://www.hotscripts.com/PHP/ – http://www.mysql.com/ – http://www.owasp.org/ – Books PHP and MySQL Web Development 2nd Edition, Welling & Thomson – Web Database Applications with PHP & MySQL, O’Reilly Publishers – PHP Cookbook, O’Reilly Publishers – MySQL Cookbook, O’Reilly Publishers – 29 Sunday 28th Jan '07
Slide 30: GNUnify '07: LAMP Workshop: hussulinux@gmail.com My special Thanks to: David Olsen (david.olsen@mail.wvu.edu) for his help in presentation's content. PHP Freaks (http://www.phpfreaks.com) This is where I learnt PHP from 30 Sunday 28th Jan '07



Add a comment on Slide 1
If you have a SlideShare account, login to comment; else you can comment as a guest- Favorites & Groups
Showing 1-50 of 6 (more)