IST 210 Database and the Web IST 210
IST 210 Database and the Web IST 210 1
IST 210 Dynamic Web Applications n Static Web application n Request with a URL (e. g. , http: //www. psu. edu) Which contains three components: protocol, web server name, and folder path to an HTML page n n Server simply send back the page From static to dynamic web pages n n Take user input and respond accordingly Allow access to information stored in a database
1. 2. 3. 4. 5. IST 210 Request to execute script Connects to the db Sends SQL to DBMS Receives Data from DBMS Creates and sends HTML to client HTML Internet HTML 1. Allows connection 2. Receives SQL 3. Returns data 3
IST 210 HTML Script SQL 4
IST 210 Server-Side Processing n n n Programs that run on the server and interact with the server through a well-defined API Reusable software components Code inside a web page that is interpreted by the web server
PHP IST 210 n n n PHP is programming model that allows dynamic, interactive Web pages to be created on the server. PHP runs in-process with the server, and is optimized to handle large volume of users. When an ‘. php’ file is requested, Web server calls PHP, which reads requested file, executes any commands, and sends generated HTML page back to browser. 6
IST 210 PHP Scripts 7
IST 210 PHP n n Server-side script language It resides on Web Server side n n You need to put your script into your account Then, any client can access it. 8
IST 210 Starting A PHP File n What can I use to write a php file? n n Any simple text editor. ie. Notepad, wordpad, pico, etc. What can’t I use to write a php file? n any formatted text editors ie. Microsoft word, etc. 9
IST 210 Starting A PHP file n The first thing to type is: <? php …. . ? > n what does “<? php” do n n what does “? >” do? n n opens a php area closes a php area whatever is enclosed in between <? php and ? > is considered to be PHP code 10
IST 210 How do I use php with a webpage? n Inside of a php document, php can intertwine with html: <html> <body> <h 2>welcome to the site</h 2> <? php echo $name; ? > please stay <? php echo $name; ? > </body> </html> 11
IST 210 How do I use php with a webpage? n n All text in between the <? php and ? > tags will be parsed as PHP All text not in between the <? php and ? > tags will be parsed as HTML 12
IST 210 Saving a php file n How do I save a php file? n n When saving choose a plain text type for file type, and save with the. php extension Example: filename. php 13
IST 210 Basic Rules n n All complete statements must end with a semicolon Similar to C in this way n These are complete statements echo “hello world”; $my. Var=“hello world”; n These are partial statements if( condition ){ while( condition){ 14
IST 210 The echo command n What is the echo command? n n n The PHP version of cout for c++ or document. write for javascript Outputs html to screen Example: <? php echo ‘hello world’; ? > n Writes “hello world” to the screen 15
IST 210 Alternate syntax for echo n There are several ways to use the echo (or mirror commands) n n echo “hello world”; echo(“hello world”); print “hello world”; print(“hello world”); 16
IST 210 Escape Characters n Escape characters are special characters that can be used to represent a character that cannot be literally represented or that would be treated as an command itself 17
IST 210 Escape Characters n Example: <? php echo ‘ I don’t know ‘; ? > n This example would cause an error because the echo command would end when the second ‘ appeared and php would not know what to do with the rest of the statement 18
IST 210 Escape Characters n n This can be prevented by adding a backslash ( ) Same example with backslash <? php echo ‘I don’t know’; ? > n This would work because php is told that any character after is to be ignored (as a 19 command)
IST 210 Escape Characters n Certain letters when used after a backslash will have special effects n n n inserts a line break t inserts a tab etc. Example: echo “hellonthow are you”; n Result: hello how are you 20
IST 210 Escape Characters n Remember, PHP is parsed on the server n n n This means that using n will not make the result go to a new line on the webpage but rather will make the result go to a new line in the source code To go to a new line on the webpage, use a tag Example: <? php echo “hellon thow are you”; ? > 21
IST 210 Comments n n Comments allow you to make comments on the code that are ignored by the server Two kinds of comments n n Single line comments Multi-line comments 22
IST 210 Comments n Single line comment: n Makes a comment that can only appear on one line //This is a single line comment n Multi-line comment: n Makes a comment that can go on until you tell it to stop /* This is a multi line comment */ 23
IST 210 Comments n Example: <? php //This will write hello world to the screen echo “hello world”; /* This script was provided courtesy of IST 210. copyright 2003 */ ? > 24
IST 210 Variables n Variables are defined with a $ n Example: n n A variable named “my. Var” would be referred to with $my. Variables are CASE SENSITIVE n This means that $my. Var and $myvar are two different variables 25
IST 210 Defining Variables n Unlike C++ you don’t have to define a variable with it’s type (int, char, etc. ) n n This is done automatically in php Here is a sample of code that defines a variable, my. Var, as the string “hello world” <? php $my. Var=“hello world”; ? > 26
IST 210 Defining Variables n Constant Variables n n n Constant variables are those that are constant and will be there throughout the whole script Constant variables are good if you have a variable that will be used in multiple scripts Constant variables cannot be redefined or undefined after being set 27
IST 210 Defining Variables n define() n n The define() function is used to define a variable as a constant Syntax: define(variable_name, variable_value); n Example: <? php define(“my. Const. Var”, “hello world”); ? > 28
IST 210 Defining Variables n You can also define a constant variable as case insensitive by using a third parameter <? php define(“my. Const. Var”, ”hello world”, TRUE); ? > n Constant variables are case sensitive by default 29
IST 210 Defining Variables n n You don’t use a $ when referring to constant variables Example: <? php define(“const. Var 1”, ”hello world”); define(“const. Var 2”, ” from psu”, TRUE); echo const. Var 1; echo const. Var 2; echo CONSTVAR 2; ? > 30
IST 210 Defining Variables n Variable Name Rules n n Must start with a letter or underscore Can only contain letters, numbers and underscores 31
IST 210 Different quotes with echo n n n Using ‘’ and “” is different with the echo command Using “” will parse the content and ‘’ will take it literally Special escape commands (like n) will only work with “” 32
IST 210 Different quotes with echo In the following script: <? php $my. Var=“hello world”; echo ‘$my. Var’; echo “$my. Var”; ? > The first echo would write $my. Var to the screen, the second would write hello world. 33
IST 210 Different quotes with echo n n Also, you don’t have to use quotes to echo a variable Example: <? php echo $my. Var; ? > 34
IST 210 Operators n n Operators can take one or more variables or values and return a new value Several Types of Operators 35
IST 210 Mathematical Operators Operator + * / % Description Example Addition - adds two variables or values $x=$y+3; together Subtraction - subtracts one variable or $y=$x-3; value from another Multiplication - multiplies two variables $z=$x*3; or values together Division – divides one variable or value $z=$x/$y; by another Modulo – divides one variable or value $r=$a%$b; by another and returns the remainder 36
IST 210 Shorthand Mathematical operators Operator += -= *= /= %= Description Example Addition – adds a variable or value to $x+=3; the original value Subtraction - subtracts a variable or value from the original value Multiplication - multiplies original variable by a variable or value $x-=3; $x*=3; Division – divides original variable by $x/=3; a variable or value Modulo – divides original variable by $x%=3; a variable or value and returns the remainder 37
IST 210 Comparison Operators Description Equal – true if $a is equal to $b Example $a==$b != Not equal – true if $a is not equal to $b $a!=$b === Identical – true if $a is equal to $b and $a===$b they are of the same type Not Identical – true if $a is not equal to $a!==$b $b or they are not of the same type Not equal – true if $a is not equal to $a<>$b $b (alternate syntax for !=) Operator == !== <> 38
IST 210 Comparison Operators cont. Description Less than – true if $a is less than $b Example $a<$b > Greater than – true if $a is greater than $b $a>$b <= Less than or equal to – true if $a is less $a<=$b than or equal to $b Greater than or equal to – true if $a is $a>=$b greater than or equal to $b Operator < >= 39
IST 210 Logical Operators && Description And – true if both $a and $b are true Example $a && $b || Or – true if either $a or $b are true $a || $b ! Not – true if $a is not true !$a and or <> And – alternate syntax for && Or – alternate syntax for || $a and $b $a or $b Xor – true if either $a or $b but not both are true $a xor $b Operator 40
IST 210 Operators n Assignment operator n n The basic assignment operator is = Example: $myvar=“hello world”; $i=0; 41
IST 210 Operators n Incrementing Operators n 4 incrementing operators n n Pre-Increment Post-Increment Pre-Decrement Post-Decrement 42
IST 210 Operators n Pre-Increment (++$a) n n Increments $a by one, then returns $a Example: <? php $a=5; echo ++$a; //this would write 6 echo $a; //this would write 6 ? > 43
IST 210 Operators n Post-Increment ($a++) n n Returns $a, then increments $a by one Example: <? php $a=5; echo $a++; //this would write 5 echo $a; //this would write 6 ? > 44
IST 210 Operators n Pre-Decrement (--$a) n n Decrements $a by one, then returns $a Example: <? php $a=5; echo --$a; //this would write 4 echo $a; //this would write 4 ? > 45
IST 210 Operators n Post-Decrement ($a--) n n Returns $a, then decrements $a by one Example: <? php $a=5; echo $a--; //this would write 5 echo $a; //this would write 4 ? > 46
IST 210 Operators n String operators n There are two string operators . and. = 47
IST 210 Operators n The. Operator n n Used to combine two strings Example: <? php $my. Var=“hello”; $my. Var=$my. Var. “ world”; echo $my. Var. ” how are you”; ? > n This echoes “hello world how are you” 48
IST 210 Operators n The. = Operator n n Used to combine two strings Example: <? php $my. Var=“hello”; $my. Var. =“ world”; echo $my. Var. ” how are you”; ? > n This echoes “hello world how are you” 49
IST 210 Conditions n n Conditional statements are statements that tell the script to do something if a condition is true Syntax: if( condition ){ action } 50
IST 210 Conditions n Example: <? php $my. Var=“hello world”; if($my. Var==“hello world”){ echo $my. Var; } ? > n since the variable $my. Var=“hello world”, the condition returns true and $my. Var is displayed on the screen 51
IST 210 Conditions n Multiple conditions n n n You can also use multiple if statements to do different things based on results There is also the else keyword which does an action if none of the if statements are true To relate more than one if statements to each other, you have to use else if 52
IST 210 Conditions n Example: <? php ? > $my. Var=“hello world”; if($my. Var==“hello world”){ echo $my. Var; } elseif($my. Var==“hello user”){ echo $my. Var; echo “goodbye”; } else{ echo “leave now!”; } 53
IST 210 Conditions n Notice that in php the elseif command is one word unlike in C where it is two words (else if) 54
IST 210 Conditions n The previous example says: n n n If $my. Var=“hello world” display $my. Var on the screen If $my. Var=“hello user” display $my. Var followed by “goodbye” on the screen Otherwise write “leave now” to the screen 55
IST 210 Loops n What are loops? n n Loops allow you to loop through something until a condition is true Different types of loops n n While loops Do-while loops Foreach loops 56
IST 210 Loops n While loops syntax: while(condition){ do action; } 57
IST 210 Loops n Example <? php $my. Var=“hello world”; $i=0; while($i<3){ echo $my. Var; $i++; } ? > 58
IST 210 Loops n The previous example will result in… hello worldhello world n While $i is less than 2, the loop keeps performing the action. n Since $i is increased by one each time through the loop, the loop will perform the action 3 times 59
IST 210 Loops n Do-While loops n Alternative syntax of a while loop do{ action }while(condition) 60
IST 210 n Loops The same loop used with while would look like this in do-while syntax: <? php $my. Var=“hello world”; $i=0; do{ echo $my. Var; $i++; } while($i<3) ? > 61
IST 210 Loops n For loops n for loops syntax: for(interval variable set; condition; interval){ action } 62
IST 210 Loops n Example: <? php $my. Var=“hello world”; for($i=0; $i<3; $i++){ echo $my. Var; } ? > n Produces same result as while loop 63
IST 210 Loops n For loop broken down: n for($i=0; Sets a variable, $i, equal to 0 n $i<3; Sets condition that needs to return false to end loop (same as condition in while(condition) ) n $i++){ Sets interval for $i to change each time through the loop, in this case it will increase by 1 each time 64
IST 210 Arrays n n n Arrays are lists of information They are created using the array() function Syntax: $my. Array=array(“a”, ”b”, ”c”, ”d”, ”e”); 65
IST 210 Arrays n Defining indexes in arrays n n If you define an array as shown in the syntax example, the first item will be array item 0, the second will be array item 1, and so on. You can tell php which number in the array (or array key) each item will be 66
IST 210 Arrays n Example: <? php $my. Array=array(“a”, 5 => “b”, 2 => “c”); ? > n Results in: $my. Array[0]=“a”; $my. Array[1]=NULL; $my. Array[2]=“c”; $my. Array[3]=NULL; $my. Array[4]=NULL; $my. Array[5]=“b”; 67
IST 210 Loops n Using for loops and while loops with arrays n n This Is an easy way to loop through an array and display its contents The sizeof() function n Finds the number of items in an array n Example: <? php $my. Array=array(“a”, ”b”, ”c”); $size_of_array=sizeof($myarray); echo $size_of_array; ? > n Result is 3 68
IST 210 Loops n Using a for loop to display an array: <? php $my. Array=array(“a”, ”b”, ”c”, ”d”, ”e”); for($i=0; $i<sizeof($my. Array); $i++){ echo $my. Array[$i]; } ? > n Result: abcde 69
IST 210 Loops n Using a while loop to display an array: <? php $my. Array=array(“a”, ”b”, ”c”, ”d”, ”e”); $i=0; while($i<sizeof($my. Array)){ echo $my. Array[$i]; $i++; } ? > n Result: abcde 70
IST 210 Loops n foreach loop n n Used with arrays Syntax: foreach(array_item as $value){ action } 71
IST 210 Loops n Example: <? php $my. Array=(“a”, ”b”, ”c”, ”d”, ”e”); foreach($my. Array as $array_item){ echo $array_item; } ? > n Result: abcde 72
IST 210 Loops n n You can also get the array key and the value using a foreach loop Example: <? php $my. Array=array(“a”, ”b”, ”c”, ”d”, ”e”); foreach($my. Array as $array_item => $array_value){ echo ‘Array item # ’. $array_item. ’ = ‘. $array_value. ”n”; } ? > 73
IST 210 Loops n Result: Array Array item item # # # 0 1 2 3 4 = = = a b c d e 74
IST 210 File Inclusions n What are file inclusions? n n Include the code from one file into another Example: 75
IST 210 File Inclusions n This is file 1. php <? php echo “hello world”; ? > 76
IST 210 File Inclusions n This is file 2. php <? php include(“file 1. php”); echo “ from psu”; ? > 77
IST 210 File Inclusions n n The result of running file 2. php will be “hello world from psu” This is because all of the code from file 1. php was put into file 2. php in place of the include() command when the code was parsed 78
IST 210 File Inclusions n n n All kinds of files can be included (html, php, etc. ) Two commands to include files: include() and require() Difference is that if the file isn’t found, include() will return a warning and move on, require() will return a fatal error and stop the script from running 79
IST 210 File Inclusions n Syntax example of include and require: include(filename); require(filename); n Example of include and require: <? php require(“check_user. php”); //check validity of user include(“header. php”); //include page header echo “content”; ? > 80
IST 210 Connect to DB 2 with PHP <? php function db_connect() { $dsn = "tsb 4"; $uid = "tsb 4"; $password = "****"; $db = odbc_connect($dsn, $uid, $password); if ($db == 0) { echo "Error: Connection problem occurred. n"; $err = odbc_errormsg($db); echo($err); return FALSE; } return $db; 81
IST 210 Simple queries in PHP $db = db_connect(); $query = "select item. Item. Name, item. Price from Sells, Item where Sells. Department. Num=1 AND Sells. Item. Num=Item. Num"; if ($db != 0) { $result = odbc_exec($db, $query); if ($result != 0) { echo "<table border=1 width="50%">"; $num = odbc_num_fields($result); 82
IST 210 Displaying queries // print column names first echo "<tr>"; for ($i=1; $i<=$num; $i++) { echo "<th>". odbc_field_name($result, $i). "</th>n"; } echo "</tr>"; // print all the value rows second while (odbc_fetch_row($result)) { if ($i%2 == 0) { echo "<tr bgcolor=lightgrey>"; } else { echo "<tr>"; } for ($i=1; $i<=$num; $i++) { echo "<td>". odbc_result($result, $i). "</td>"; } echo "</tr>n"; } echo "</table>n"; } else { echo "Sorry, your search did not return any results. "; 83
IST 210 Don't forget this odbc_close($db); ? > 84
IST 210 Resources n PHP Official site http: //www. php. net n PHP Official documentation http: //www. php. net/manual/en/ 85
- Slides: 85