CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
PL/SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP.
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, October 18, 2012 Session 7: PHP.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
CMPE 226 Database Systems September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
CS 174: Web Programming September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CS 174: Web Programming August 31 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
PHP PHP: Hypertext Preprocesor Personal Home Page Tools.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Fall 2004EE 3563 Digital Systems Design EE 3563 VHDL – Basic Language Elements  Identifiers: –basic identifier: composed of a sequence of one or more.
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
CMPE 226 Database Systems September 2 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
CS 174: Web Programming November 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CMPE 226 Database Systems February 16 Class Meeting Department of Computer Engineering San Jose State University Spring 2016 Instructor: Ron Mak
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
CMPE 226 Database Systems February 8 Class Meeting Department of Computer Engineering San Jose State University Spring 2016 Instructor: Ron Mak
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
Web Database Programming Using PHP
CMPE 226 Database Systems February 14 Class Meeting
ISC440: Web Programming 2 Server-side Scripting PHP 3
CS 174: Server-Side Web Programming February 14 Class Meeting
PHP.
Web DB Programming: PHP
CS 174: Server-Side Web Programming February 19 Class Meeting
MySQL Web Application Connecting to a MySQL database
PHP an introduction.
Presentation transcript:

CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Teams 2

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Three-Tier Web Application Architecture 3 Client-side web browser Server-side web server (.html.php images, etc.) Form data Back-end database server (MySQL) Queries Data Dynamically generated web pages

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Default Web Page: index.html 4 <form action="queryDB.php" method="get"> User input First name: Last name: For Assignment #1, you should have more than just input text fields in your HTML form!

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Page: queryDB.php 5 Query Results <?php $first = filter_input(INPUT_GET, "firstName"); $last = filter_input(INPUT_GET, "lastName"); try { // Connect to the database. $con = new PDO("mysql:host=localhost;dbname=supercoders", "supercoders", "sesame"); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);... } catch(PDOException $ex) { echo 'ERROR: '.$ex->getMessage(); } ?> PDO documentation: queryDB.php Connect to the database and set $con to refer to the connection object. If an error occurs, throw an exception.

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Page: queryDB.php, cont’d 6 try {... $query = "SELECT * FROM people"; // We're going to construct an HTML table. print " \n"; // Fetch the database field names. $result = $con->query($query); $row = $result->fetch(PDO::FETCH_ASSOC); // Construct the header row of the HTML table. print " \n"; foreach ($row as $field => $value) { print " $field \n"; } print " \n";... } Query the database to get a result set $result. Fetch one $row from the result set as an associative array of field:value elements. Print the $field name. queryDB.php

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Page: queryDB.php, cont’d 7 try {... // Constrain the query if we got first and last names. if ((strlen($first) > 0) && (strlen($last) > 0)) { $query = "SELECT * FROM people ". "WHERE first = '$first' ". "AND last = '$last'"; } // Fetch the matching database table rows. $data = $con->query($query); $data->setFetchMode(PDO::FETCH_ASSOC);... } Query the database again to get the result set $data. queryDB.php

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Page: queryDB.php, cont’d 8 try {... // Construct the HTML table row by row. foreach ($data as $row) { print " \n"; foreach ($row as $name => $value) { print " $value \n"; } print " \n"; } print ” \n";... } Loop over each $row of the result set $data. Loop over the name:value elements of each $row. Print the $value. queryDB.php

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Setting the Fetch Mode  Instead of  You can write instead 9 $result = $con->query($query); $row = $result->fetch(PDO::FETCH_ASSOC) $result = $con->query($query); $result->setFetchMode(PDO::FETCH_ASSOC); $row = $result->fetch()

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Single-Query Alternative 10 print " \n"; // Query the database. $data = $con->query($query); $data->setFetchMode(PDO::FETCH_ASSOC); // Construct the HTML table row by row. // Start with a header row. $doHeader = true; foreach ($data as $row) {... } print " \n”; queryDB2.php

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Single-Query Alternative, cont’d 11 // Construct the HTML table row by row. // Start with a header row. $doHeader = true; foreach ($data as $row) { // The header row before the first data row. if ($doHeader) { print " \n"; foreach ($row as $name => $value) { print " $name \n"; } print " \n"; $doHeader = false; }... } queryDB2.php

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Single-Query Alternative, cont’d 12 // Construct the HTML table row by row. // Start with a header row. $doHeader = true; foreach ($data as $row) { // The header row before the first data row.... // Data row. print " \n"; foreach ($row as $name => $value) { print " $value \n"; } print " \n"; } queryDB2.php

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Syntax  Very similar to C. End each statement with a semicolon.  Case sensitive: variables, constants, array keys class properties and constraints  Case insensitive: functions (pre-defined and user-defined) class constructors and methods reserved words 13

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Variables  All variable names start with $.  PHP is a dynamically typed language. You don’t declare a variable’s type. A variable can be assigned a value of any type.  PHP data types scalar: integer, float, boolean, string array object resource NULL 14

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Strings  Enclose a string with single or double quotes. Examples:  Variables embedded in a double-quoted string are evaluated: But not: 15 "Hello, world!" 'Hello, world!' "It's a nice day." 'Define "string" for me.' "Define \"string\" please." "The first name is $first." 'The first name is $first.'

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP String Operations  The string concatenation operator is.  Some string functions: strlen() strtoupper() strtolower() ucwords() capitalize the first letter of every word 16 Demo $name = $last. ", ". $first; $name.= ", Esq.";

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Heredocs  Use a heredoc to avoid string quoting issues. Example: 17 $first = "John"; $last = "Smith"; print <<<HERE First name: $first Last name: $last HERE; Must be on a line by itself with no indentation. Demo

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Constants  Name constants with all uppercase letters, by convention. Constants are not variables, so do not use $.  Examples But not: 18 define (PI, ); define (HOST_NAME, "localhost"); print "Host name is ". HOST_NAME; print "Host name is HOST_NAME";

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Two Kinds of PHP Arrays  Indexed array Indexes are integers.  Associative array Indexes are strings. key-value pairs, like a hash table. 19

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Creating PHP Indexed Arrays  Use the array() function:  Specify the first index value. Subsequent elements are indexed incrementally.  An array of sequential numbers: 20 $bands[] = "Beatles"; $bands[] = "Rolling Stones"; $bands[] = "Queen"; $bands = array("Beatles", "Rolling Stones", "Queen"); $bands = array(2=>"Beatles", "Rolling Stones", "Queen"); $values = range(5, 10);

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Creating PHP Associative Arrays  Use the array() function: 21 $states["CA"] = "California"; $states["NY"] = "New York"; $states["TX"] = "Texas"; $states = array( "CA" => "California", "NY" => "New York", "TX" => "Texas" ); An associative array is like a hash table.

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Looping over Array Elements  Use the foreach statement: Examples: 22 foreach ($ arrayname as $ variable ) { … } foreach ($ arrayname as $ key => $ value ) { … } foreach ($bands as $bandName) { print $bandName; } foreach ($states as $abbrev => $fullName) { print "State $fullName is abbreviated $abbrev"; } Demo

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Multidimensional Arrays 23 $north = array("ND" => "North Dakota", "MN" => "Minnesota"); $south = array("TX" => "Texas", "FL" => "Florida"); $east = array("NY" => "New York", "ME" => "Maine"); $west = array("CA" => "California", "OR" => "Oregon"); $us = array( "N" => $north, "S" => $south, "E" => $east, "W" => $west ); Demo

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Functions  Syntax for programmer-defined functions: Examples:  A function can optionally return a value. 24 function name ( optional arguments ) { // statements in the body } function doSomething() { … } function sayHello($first, $last) { … } function greet($name, $language = "English") { … } function calculate($input, &$output) { … } return value ; Default value Passed by reference

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak Scope of PHP Variables  Variables have the scope of the PHP file in which they reside.  A programmer-defined function creates a scope for its variables. Variables defined in a function cannot be accessed outside the function. Variables defined outside the function are not accessible inside the function.  Use the global statement inside a function to access outside variables. Example: 25 global $outsideVar;

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PHP Data Objects (PDO)  Create a database abstraction layer: 26 PostgresMySQLOracle PHP Data Objects (PDO) PHP query() PDO documentation:

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PDO Examples  Create a new PDO object to represent the database connection.  Set the error mode attribute to throw an exception if there is an error. 27 // Connect to the database. $con = new PDO("mysql:host=localhost;dbname=supercoders", "supercoders", "sesame"); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PDO Examples, cont’d  PDO::query() executes an SQL statement and returns a result set as a PDOStatement object.  PDOStatement::fetch() fetches the next row of the result set. PDO::FETCH_ASSOC returns the row as an associative array indexed by column names. 28 // Fetch the database field names. $result = $con->query($query); $row = $result->fetch(PDO::FETCH_ASSOC);

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PDO Examples, cont’d  Extract the column (field) names of the fetched row to construct the header row of the HTML table. 29 // Construct the header row of the HTML table. print " \n"; foreach ($row as $field => $value) { print " $field \n"; } print " \n";

Computer Science Dept. Spring 2015: February 3 CS 174: Web Programming © R. Mak PDO Examples, cont’d  PDOStatement::setFetchMode sets the default fetch mode for this statement. 30 // Fetch the matching database table rows. $data = $con->query($query); $data->setFetchMode(PDO::FETCH_ASSOC); // Construct the HTML table row by row. foreach ($data as $row) { print " \n"; foreach ($row as $name => $value) { print " $value \n"; } print " \n"; }