You are on page 1of 62

PHP MYSQL

csc492

Website development tools Languages

Website development tools - Graphic


Adobe Photoshop Premier After Effect Macromedia Free Hand Fireworks Dreamweaver Director Flash Combustion 3d max /maya Combustion Maya Editing Photos Editing Movies (avi) Special effects in the Movie Drawing / Logo Editing Photos Web site CD Presentations 2 D Animation Special Effects 3 D Animation

We require
Web Server (Apache) PHP Database Text Editor (Dreamweaver) Web Browser

Structure

What is PHP? Why PHP?

What is PHP?
Server-side, scripting language
Event based

Designed for use with HTML


File Name .php

Provide more flexibility than HTML alone Syntax is similar to C, Java, Perl

PHP History
PHP succeeds an older product, named PHP/FI. PHP/FI was created by Rasmus Lerdorf in 1995. Personal Home Page/ Forms Interpreter - First Public release Version 3: 1997 PHP: Hypertext Preprocessor created by Andi Gutmans and Zeev Suraski Version 4: 2000 - Zend, full feature Actively supported by updates Version 4.4.6 on March 1, 2007 Version 5: 2004 New improved, Object Oriented & database connectivity The latest version Version 5.2.1 Stable

Why Use PHP?


Limits of HTML Open source & free ZEND is doing great! Job. Cross platform to develop, to deploy and to use:
Window Macintosh Linux

Powerful, Robust, scalable (Handle the traffic grow with you) Web development specific (Personal web site) Can be object oriented, especially in version 5 Great documentation in many languages

Documentation PHP?

http://www.php.net http://docs.php.net/manual/en/getting-started.php

Windows Installation
PHP requires:
Windows 98/Me or Windows NT/2000/XP/2003

Apache:
http://httpd.apache.org/

PHP:
http://www.php.net

MySQL:
Dev.mysql.com

Windows Installation All-in-one Packages

WAMP:
www.wampserver.com/en/index.php

XAMPP:
www.apachefriends.org

Ill be using:
XAMPP 1.6.6a
Apache HTTPD 2.2.8 + Openssl 0.9.8g MySQL 5.0.51a PHP 5.2.5 PHP 4.4.8 phpMyAdmin 2.11.4

Note: show settings Dreamweaver Text Editor Web Browser (Firefox)

Text Editor
Dreamweaver, Zend Studio, NotePad Word processors are not good for PHP editing PHP designer Best editors have:
Syntax coloring/highlights Syntax hinting

Popular PHP software


Blog/ CMS: WordPress, PHP blog, Drupal, CMSimple, Joomla, PHP website, Drupal, PHP(Reactor) School System: Moodle Forum: Prado, phpBB, Vanilla, BBPress

PHP Programming
<?php ?>

Short syntax

<?php phpinfo(); ?>

Note: create first first.php firstphp.swf

Echo/ Print function


echo

or & concatenation Note: hello.php hello2.php

Comments
It is good practice to enter comments
//single-line comments are like this #or like this /* Multiple line comments*/

Variables Names
In PHP variable starts with a $ Followed by letter or underscore Can contain letters, numbers, underscores, or dashes No spaces Case-sensitive $student and $Student are different as it is case sensitive Global variable global $student Superglobals Automatically available throughout all program $_GET $_POST $_Request $_COOKIES $_SESSION

Examples of Variable Names:


$employee $Employee $NicEmployee (camel case) $Nic_Employee $Nic-Employee (Not a good practice) $Employee3 $_employee (Not a good practice) $__employee (Not a good practice)

Storing strings and numbers in variables

Note: variable1.php and variable2.php

Strings:

Strings concatenate:

Note: strings.php

Strings function:

Note: strings2.php

Basic Math:

Note: basic.php

Float

Float

IF statement
If (expression)
Statement;

If($a>$b) {
Echo a is larger than b; }
Note: ifstatement.php

Array

Note: arrays.php

While Loop:
While (expression)
Statement;

While ($count<=10) {
Echo $count; $count++; Note: while.php }

Same example with for Loop:


For (Initial; expression; end)

Note: for.php

Foreach Loop:
Foreach ($array as $value) Statement;

Note: foreach.php

Continue and Break:

Note: continue.php

User defined Function


// Value passes before the code executes is called arguments Function name ($arguments) Statement;

Note: function.php

Function arguments

Predefined Function

Note: prefunction.php

Boolean and Null

Note: boolean.php

Constants

Note: constant.php

Overview of phpMyAdmin

Creation of a new DB Insertion of a record

CRUD Create, Read, Update, Delete

Note: mysql1.php

Insert

Update

Delete

Create Form

Note: form.html

Methods to derive data from User


URLs/Links Forms Cookies GET Post Cookie

Super global $_GET


$_GET is used for data passes using the HTTP GET method, which includes variables passes as part of the URL www.yahoo.com/index.php?id=1&name=stuti $_GET has been around since PHP 4.1.0. Before that, $HTTP_GET_VARS was used though this was not automatically global.

Super global $_GET


Example to show that all the value covered after ? Is stored in the get array. Get is a Super global array

Super global $_GET


Explanation: dynamic web site name before question mark then to enCODE we will use rawurlencode

Super global $_POST


The HTTP POST method is very similar to the $_GET. It is conventionally used when the contents of an HTML form are going to change values in a database table or make some other permanent change. $_POST has been around since PHP 4.1.0. Before that $HTTP_POST_VARS was used though this was not automatically global.

Connect Form to MySQL


Take the POST variables Escape strings Connect to the MySQL server Do the insert Disconect from the MySQL server

PEAR
PEAR is a framework and distribution system for reusable PHP components. What is PEAR? PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide: A structured library of open-source code for PHP users A system for code distribution and package maintenance A standard style for code written in PHP The PHP Extension Community Library (PECL), A web site, mailing lists and download mirrors to support the PHP/PEAR community PEAR is a community-driven project governed by its developers. PEAR's governing bodies are subdivided into the PEAR Group, Collectives, and a President. PEAR's constitution (adopted in March 2007) defining these groups is documented Many components/ packages for PHP can be downloaded from http://pear.php.net/, installed and then the functionality can be used. http://pear.php.net/packages.php PEARDb DB abstraction layer http://pear.php.net/package/DB/ Useful for cases where we might foresee a change in DB backend This package has been superseded, Use MDB2 instead. http://pear.php.net/package/MDB2

An Introduction to JOOMLA Language

Stuti Kumar

Include function
Explanation: The function include() allows embedding one file inside another. Difference between require and include

Note: include.php, include.html

COOKIES
Explanation: The third form of deriving information from the user is cookies. The syntax used to write is : Setcookies($name,$value,$expire); <?php setcookie(testNIC, id45, time()+(60*60*24*7)); ?> Code explanation: Name testNIC, ID is 45, current time time() + 60 sec * 60 min * 24 hours * 7 days

Unset a Cookie
<?php setcookie(testNIC, id45, time()-(60*60*24*7)); ?>

Session
A cookie stores variables on the visitor's machine whereas a session stores the variables on the server and just gives the visitor a cookie with an ID corresponding to those variables. Advantage to use session:
Creates a file on the webserver Can store lot of information Long code & complicated for user to hack hence more secure.

Dynamic Forms using PHP


How can we get dynamic data into a form Create table departments using PHPMyAdmin Create table employees See: form.php Input user details Submit to itself User details added to the DB

User form
Note: user_add.php Note: user_edit.php Hidden variable

File Functions
fopen 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. 'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it.

File Permissions
Chmod change permissions of files. <?php // Read and write for owner, nothing for everybody else chmod("/somedir/somefile", 0600); // Read and write for owner, read for everybody else chmod("/somedir/somefile", 0644); // Everything for owner, read and execute for others chmod("/somedir/somefile", 0755); // Everything for owner, read and execute for owner's group chmod("/somedir/somefile", 0750); ?>

File Ownership
Chown change owner of files. <?php // change owner to user suchi chown("/somedir/somefile", suchi); ?>

File Locking
flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix derivatives and even Windows)

You might also like