1.23k likes | 2.05k Views
INTRODUCTION TO PHP/ mySQL I. FTSM Lab / May 2011. Goal. Provide the basic knowledge of PHP programming Explain how you can code and run PHP scripts Creating dynamic pages Basic database interactions – read, insert, update and delete data Basic session management PHP Application packages.
E N D
INTRODUCTION TO PHP/mySQL I FTSM Lab / May 2011
Goal • Provide the basic knowledge of PHP programming • Explain how you can code and run PHP scripts • Creating dynamic pages • Basic database interactions – read, insert, update and delete data • Basic session management • PHP Application packages
Structure HTML/ XHTML PHP Database CSS JavaScript
HTML/JavaScript/CSS • http://www.w3schools.com • HTML, JavaScript and CSS tutorials
What is PHP? • PHP = ‘Hypertext PreProcessor’ • Originally created by Rasmus Lerdorf in 1994 • The main implementation of PHP is now produced by The PHP Group (de facto standard for PHP) - http://www.php.net/ • Open-source (access to source code and free distribution right), server-side scripting language
Webhosting • Webhosting information
WAMP Packages - XAMPP • http://www.apachefriends.org/en/index.html • Version for Windows includes: Apache, MySQL, PHP, Perl, phpMyAdmin, JpGraph, FileZilla FTP Server, SQLite etc.
WAMP Packages - WAMPServer • http://www.wampserver.com/en/ • Version for Windows includes: Apache, PHP, Mysql (version 64 and 32 bits), PhpMyadmin, SQLBuddy, XDebug, webGrind, XDC
PHP Scripts Basic syntaxes, data types, variable, control structures, arrays, function
PHP VARIABLES BASIC SYNTAX DATA TYPE CONTROL STATEMENTS FUNCTION DATABASE CONNECT/ READ INSERT UPDATE DELETE SESSION PHP PACKAGES
PHP code • Structurally similar to C/C++ • All PHP statements end with a semi-colon • Each PHP script must be enclosed in the reserved PHP tag <?php … …?>
PHP code - comments • Standard C, C++, and shell comment symbols // C++ and Java-style comment # Shell-style comments /* C-style comments These can span multiple lines */
PHP code - output • Use ‘echo’ or ‘print’ • Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP <?php $nilai= 25; // Numerical variable$ayat= “Hello”; // String variable echo $ayat; // Outputs Hello echo $nilai, $ayat; // Outputs 25Hello echo “5x5=”,$nilai; // Outputs 5x5=25 echo “5x5=$nilai”; // Outputs 5x5=25echo ‘5x5=$nilai’; // Outputs 5x5=$nilai ?>
PHP – escape character • If the string has a set of double quotation marks that must remain visible, use the \ [backslash] before the quotation marks to ignore and display them. <?php $jab=“\”PHP\””; print $jab; //”PHP” ?>
PHP code - variables • PHP variables must begin with a “$” sign • Case-sensitive ($var != $VAR != $vAr) • Global and locally-scoped variables • Global variables can be used anywhere • Local variables restricted to a function or class • Certain variable names reserved by PHP • Form variables ($_POST, $_GET) • Server variables ($_SERVER)
PHP code – variables <?php $nilai= 25; //Numerical variable$ayat= “Hello”; //String variable $nilai= ($nilai* 7); //Multiplies variable nilai by 7 ?>
PHP code - operations <?php $a=30; $b=5; $total=$a+$b; print $total; //35 print “<p>Jumlahialah $total</p>”; // Jumlahialah 35 print $a-$b; //25 print $a*$b; //150 print $a/$b; //6 print $a+=$b; //35 print $a%$b; //0 ?>
PHP code – strings function • Use a period to join strings into one. <?php $string1=“Hello”; $string2=“PHP”; $string3=$string1 . “ ” . $string2; print $string3; //Hello PHP ?>
PHP – Control Statements • Control structures similar with JavaScript/C++ • if, elseif, else • switch, case • while • for • foreach
PHP - if, elseif, else <?php $markah = 90; if ($markah >= 80) echo “Lulus dengancemerlang";elseif ($markah >= 40) echo “Lulus";else echo “Gagal"; ?>
PHP control – switch … switch ($jantina){ case “L”: echo “Lelaki"; break; case “P”: echo “Perempuan"; break; default: echo “Tiada input jantina"; }…
PHP control – while loops <?php $nombor = 1; while ($nombor!=10){ print “Bilangan $nombor”; $nombor++; } ?>
PHP control – for loops <?php for ($n = 1; $n<=10; $n++){ print “Bilangan $n”; } ?>
PHP control – foreach loops <?php $numbers = array("one","two","three"); foreach ($numbers as $value) { echo $value . "<br />"; } ?>
PHP arrays • Three kind of arrays: • Numeric array - An array with a numeric index • Associative array - An array where each ID key is associated with a value • Multidimensional array - An array containing one or more arrays
PHP – numeric arrays <?php //numeric array $cars = array("Saab","Volvo","BMW",“Ford"); echo $cars[2]; //BMW ?>
PHP – associative arrays <?php //associative array $umur = array ("Raju"=>32, "Gopal"=>34, "Samy" => 36); //same as $umur[‘Raju’]=32… echo $umur[‘Gopal’]; //34 ?>
PHP - multi dimensional arrays <?php //multidimensional array $kump = array ("Merah"=> array ("Ali", "Raju", "Joan"), "Biru"=> array ("Abu", "Jason", "Lin"), "Hijau" => array ("David", "Jim", "Mary"); echo $kump [‘Merah’][2]; //Joan echo $kump [‘Hijau’][0]; //David ?>
PHP array functions • array_push() – adds element/s to an array <?php $a=array("Dog","Cat");array_push($a,"Horse","Bird");print_r($a); /*Array ([0]=>Dog [1]=>Cat [2]=>Horse [3]=>Bird) */ ?>
PHP array functions • array_pop() – deletes last element in an array <?php $a=array("Dog","Cat","Horse");array_pop($a);print_r($a); // Array ([0]=>Dog [1]=>Cat) ?>
PHP array functions • unset() – destroy a variable $array = array(0, 1, 2, 3); unset($array[2]); /* array(3) { [0]=>int(0) [1]=>int(1) [3]=>int(3) } */
PHP - functions • Functions MUST be defined before they can be called • Function headers are of the format • function function_name ($var1, $var2…){ • … • } • Function names are not case sensitive
PHP - functions <?php // This is a function function darab($arg1, $arg2){ $arg3 = $arg1 * $arg2; return $arg3; } echo darab(12,3); // 36 ?>
PHP - Using external files • Using external files for: • HTML codes • Structure – template files like headers, footers • Functions – separate file to store all functions • Config – separate configuration settings in different file
PHP – include and require • Four functions: • include() • include_once() • require() • require_once() Generates warnings when the function doesn’t work Generates errors and halts scripts when the function doesn’t work
PHP - include • Using “include” to include external files <?php include “header.php” Include “tarikh.php” include “menubar.php” … ?> <?php print “Tarikhhariiniialah $date2”; ?>
PHP References • http://www.php.net <- php home page • http://www.php.net/downloads <- php download page • http://www.php.net/manual/en/install.windows.php <- php installation manual • http://www.w3schools.com/php/default.asp <-php online tutorial
Database and SQL SQL, MySQL, phpMyAdmin, creating database and tables
SQL • SQL – Structured Query Language • SQL can be used to access and manipulate databases SELECT * FROM pelajar SELECT * FROM pelajar WHERE NoMatrik=‘A123456’
SQL Queries • Query database for specific information and have a recordset returned from table SELECT nama FROM pelajar
SQL Keywords • SELECT – select from tables • FROM – specifies tables • WHERE – specifies criteria • INSERT – inserts data into table • UPDATE – updates data in table • DELETE – deletes data in table • CREATE – create new table • DROP – delete existing table
SQL Keywords Usage SELECT * FROM pelajar Output: select all columns from table pelajar SELECT nama, nomatrik FROM pelajar Output: select columns nama and nomatrik from table pelajar SELECT nama, nomatrik FROM pelajar WHERE nomatrik=‘A12345’ Output: select columns nama, nomatrik from table pelajar where row nomatrik equals ‘A12345’
SQL Keywords Usage INSERT INTO pelajar (nomatrik, nama, jabatan, kumpulan) VALUES (‘A12365’, ‘Hashim’, ‘4’, ‘9’) Action: insert specified value to table pelajar, creating new row DELETE FROM pelajar WHERE nomatrik=‘A12369’ Action: select row from table pelajar where nomatrik as specified and delete the row
SQL Keywords Usage UPDATE pelajar SET nama = ‘Hisham’ WHERE nama = ‘Hashim’ AND nomatrik = ‘A12365’ Action: update specified row to table pelajar
MySQL • MySQL - "My Structured Query Language“ • Created by Michael Widenius from TcX (Sweden) in 1994 • Open-source, relational database management system • MySQL is used in web applications and acts as the database component of the WAMP/LAMP • Used in free software projects (e.g. WordPress, Joomla)
MySQL and WAMP/LAMP • Download at www.mysql.com
MySQL Interfaces • Interfaces to manage and browse database easily • phpMyAdmin • heidiSQL • MySQL-Front • SQLyog