M.P. Johnson, DBMS, Stern/NYU, Sp20041 C20.0046: Database Management Systems Lecture #21 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

PHP I.
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #20 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Copyright 2004 Monash University IMS5401 Web-based Systems Development Topic 2: Elements of the Web (g) Interactivity.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #20 M.P. Johnson Stern School of Business, NYU Spring, 2005.
A simple PHP application We are going to develop a simple PHP application with a Web interface. The user enters two numbers and the application returns.
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #22 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #17 M.P. Johnson Stern School of Business, NYU Spring, 2008.
Python and Web Programming
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #21 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #16 M.P. Johnson Stern School of Business, NYU Spring, 2008.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
PHP: Introduction By Trevor Adams.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
1 Web Database Processing. Web Database Applications Static Report Publishing a report is prepared from a database application and exported to HTML DB.
JAVASCRIPT HOW TO PROGRAM -2 DR. JOHN P. ABRAHAM UTPA.
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Server-side Scripting Powering the webs favourite services.
INTERNET APPLICATION DEVELOPMENT For More visit:
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
PHP meets MySQL.
Lecture # 6 Forms, Widgets and Event Handling. Today Questions: From notes/reading/life? Share Personal Web Page (if not too personal) 1.Introduce: How.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Putting it all together Dynamic Data Base Access Norman White Stern School of Business.
Website Development with PHP and MySQL Saving Data.
Basic & Advanced Reporting in TIMSNT ** Part Two **
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
CS 4720 Dynamic Web Applications CS 4720 – Web & Mobile Systems.
Computational Boot Camp HTML Mike Schaffer. 8/23/2002 (MES) HTML What is HTML? HTML stands for HyperText Markup Language HTML is the language for publishing.
CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
Introduction to JavaScript CS101 Introduction to Computing.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
1) PHP – Personal Home Page Scripting Language 2) JavaScript.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
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.
PHP Form Processing * referenced from
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
Section 10.1 Define scripting
CS 330 Class 7 Comments on Exam Programming plan for today:
Section 6.3 Server-side Scripting
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP / MySQL Introduction
Lecture 5: Functions and Parameters
Web Application Development Using PHP
Presentation transcript:

M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #21 Matthew P. Johnson Stern School of Business, NYU Spring, 2004

M.P. Johnson, DBMS, Stern/NYU, Sp Agenda Previously: CGI, Perl Next: Scripting for SQL on the web  More Perl  PHP Security Project part 4 due today Project part 5 assigned today  Short hw?

M.P. Johnson, DBMS, Stern/NYU, Sp Review: CGI Program Client Server HTTP Request Data for program Generated HTML HTML Image from

M.P. Johnson, DBMS, Stern/NYU, Sp Perl and HTML headers Data sent to a browser is prefaced with a header describe type of data: Hand-generated html must print this before anything else: Or:  When use-ing CGI Content-type: text/html\n\n print “Content-type: text/html\n\n”; print CGI::header();

M.P. Johnson, DBMS, Stern/NYU, Sp Perl, HTML, and CGI.pm CGI.pm offers a “front-end” to HTML  Replaces mark-up language with an API Very simple example:  gipm.cgi gipm.cgi Somewhat simpler, but another thing to learn Mostly won’t cover Review: Hello, World

M.P. Johnson, DBMS, Stern/NYU, Sp New topic: HTML forms “Active” parts of HTML: forms Intuition for name: paper form  Fill in textboxes, check boxes or not, etc.  Turn it in HTML form  contains arb. # of INPUTs  Submits to somewhere (ACTION)  By GET or POST

M.P. Johnson, DBMS, Stern/NYU, Sp Form example (visible?) From On clicking Send, we go to script.php with “foo=bar”

M.P. Johnson, DBMS, Stern/NYU, Sp Perl and forms Obtain param number: Goal: display text and button;  On submit, tell user what was entered Improve: also print, say, double the input my $cgi = CGI->new(); $param = $cgi->param('number'); my $cgi = CGI->new(); $param = $cgi->param('number');

M.P. Johnson, DBMS, Stern/NYU, Sp Perl error-handling Many Perl scripts have lines of the form  some-statement OR die(“something happened”); What this means:  die exits with error message  Perl supports both || and OR as or operator  Perl supports boolean “short-circuiting” Boolean eval stops as fast as possible  Ftns often return 0/null/false for errors  if some-statement fails then we die

M.P. Johnson, DBMS, Stern/NYU, Sp Perl and databases DB connectivity is done through DBI  Database Interface  Analogous to Java’s JDBC Think of DBI as a Java class with static methods Use these to obtain a connection, prepare and execute queries, etc.

M.P. Johnson, DBMS, Stern/NYU, Sp Perl DBI 1. Open a connection: 2. Prepare and execute query: my $dbh = DBI-> connect("dbi:mysql:database=test;mysql2.st ern.nyu.edu;port=3306", user, pass); my $sth = $dbh->prepare($query); $sth->execute; my $sth = $dbh->prepare($query); $sth->execute;

M.P. Johnson, DBMS, Stern/NYU, Sp Perl DBI 3. Extract next row of data from statement results, if available:  What this means: row has two fields, whose values are put in $a and $b, in order  Other options, but this should suffice In general, want to scroll through results:  Braces { } are required! my ($a, $b) = $sth->fetchrow_array() while (my ($a, $b) = $sth->fetchrow_array()) { # print out $a and $b } while (my ($a, $b) = $sth->fetchrow_array()) { # print out $a and $b }

M.P. Johnson, DBMS, Stern/NYU, Sp Limit: Perl webpages that do something Semi-interesting Perl script:  Non-trivial but not huge: ~40 lines Works with two-column (a,b) table  Takes input from user  Returns rows whose a field contains value  If no/empty input, returns all rows Bad idea in general!

M.P. Johnson, DBMS, Stern/NYU, Sp lookup.cgi Two possible situations for running script: 1. Page opened for the first time 2. User entered parameter and pressed button Structure of file: 1. Print input box and button for next search  On button click, parameter is sent to this page’s url 2. (Try to) read input parameter 3. Open MySQL connection 4. Run query 5. Print results in a table 6. Disconnect from MySQL

M.P. Johnson, DBMS, Stern/NYU, Sp Higher-level structure As one page:  If we have params, display data based on them  Otherwise, prompt user for params, call self Could be:  Page 1: prompt for params, call page 2  Page 2: display data based on params In e.g.: always display data for convenience

M.P. Johnson, DBMS, Stern/NYU, Sp Tutorials on Perl Some material drawn from the following good tutorials: CGI backend programming using perl:  Perl Basics:  CGI Basics:  MySQL/Perl/CGI example: 

M.P. Johnson, DBMS, Stern/NYU, Sp That’s all, folks! Q: Is this enough to get a job coding Perl? A: Probably not! But: Don’t like Perl/CGI? Don’t want to run start a process for every user of your site? Next we’ll do PHP… a couple modified copies of lookup.cgi and cia.cgi + some HTML  fairly interesting site a couple modified copies of lookup.cgi and cia.cgi + some HTML  fairly interesting site

M.P. Johnson, DBMS, Stern/NYU, Sp Dynamic webpages Original prob: need webpages to respond to user inputs Soln 2:  create a an html file embedded with special non- html code  upon url request, execute embedded code to generate more html  Send back the modified html page to user  An incomplete html page exists on server  PHP, JSPs, ASPs, etc.

M.P. Johnson, DBMS, Stern/NYU, Sp New topic: PHP First option: for each request: run program, produce whole page, send back  CGI and some host language Second option: create html page with missing parts; for each response, fill in the wholes and send back  Embedded scripting  PHP and others  PHP = Personal Home Page or = PHP Hypertext Processor

M.P. Johnson, DBMS, Stern/NYU, Sp hello.php Q: What the difference between and \n? Hello from PHP Here is the PHP part: \n“; ?> That's it! Hello from PHP Here is the PHP part: \n“; ?> That's it!

M.P. Johnson, DBMS, Stern/NYU, Sp hello2.php Script errors, w/ and w/o display_errors on:   Local dir must contain.htaccess:  Automatically load GET/POST params as vars  php_flag display_errors on php_flag register_globals on php_flag display_errors on php_flag register_globals on

M.P. Johnson, DBMS, Stern/NYU, Sp More on PHP Somewhat C-like, somewhat Perl-like Case-sensitive Comments:  # Unix shell-style  /* */ C-style  // C++-style Output:  echo(“hi there”);  C’s printf

M.P. Johnson, DBMS, Stern/NYU, Sp PHP vars Similar to those of Perl  <? $num1 = 58; $num2 = 67; print "First number ". $num1. " "; print "Second number ". $num2. " "; $total = $num1 + $num2; print "The sum is ". $total. " "; ?> <? $num1 = 58; $num2 = 67; print "First number ". $num1. " "; print "Second number ". $num2. " "; $total = $num1 + $num2; print "The sum is ". $total. " "; ?>

M.P. Johnson, DBMS, Stern/NYU, Sp Combining PHP and HTML <?php for($z=0;$z<=5;$z++) { ?> Iteration number <? } ?> <?php for($z=0;$z<=5;$z++) { ?> Iteration number <? } ?>

M.P. Johnson, DBMS, Stern/NYU, Sp PHP info PHP does not have both string and number ops like Perl Number ops treat (number) strings as numbers, regular strings as strings  Info function displays lots of PHP/HTML info: 

M.P. Johnson, DBMS, Stern/NYU, Sp PHP & MySQL 1. Open a connection and open our DB: 2. Run query: $db = mysql_connect("mysql2.stern.nyu.edu:3306", user, pass); mysql_select_db("test", $db); $db = mysql_connect("mysql2.stern.nyu.edu:3306", user, pass); mysql_select_db("test", $db); $result = mysql_query($query,$db);

M.P. Johnson, DBMS, Stern/NYU, Sp PHP & MySQL 3. Extract next row of data from statement, if available:  What this means: myrow is an array that can then be accessed  Other options, but this should suffice In general, want to scroll through results: $myrow = mysql_fetch_row($result) while ($myrow = mysql_fetch_row($result)) # print row’s data while ($myrow = mysql_fetch_row($result)) # print row’s data

M.P. Johnson, DBMS, Stern/NYU, Sp Limit: PHP webpages that do something Semi-interesting Perl script:   Non-trivial but not huge: ~60 lines, but much plain html Works with two-column (a,b) table Takes input from user Returns rows whose a field contains value If no/empty input, returns all rows  Bad idea in general!

M.P. Johnson, DBMS, Stern/NYU, Sp lookup.php: port of lookup.cgi Two possible situations for running script: 1. Page opened for the first time 2. User entered parameter and pressed button Structure of file: 1. Print input box and button for next search  On button click, parameter is sent to this page’s url 2. (Try to) read input parameter 3. Open MySQL connection 4. Run query 5. Print results in a table 6. Disconnect from MySQL

M.P. Johnson, DBMS, Stern/NYU, Sp Insert/delete Perl/PHP example Similar to search example NB: form has two buttons t t

M.P. Johnson, DBMS, Stern/NYU, Sp Master-detail Perl/PHP example Idea: display list of regions;  When region clicked on, display its countries Mechanism: pass GET param in link, not with a FORM s/cia.pl s/cia.pl s/cia.php.txt s/cia.php.txt

M.P. Johnson, DBMS, Stern/NYU, Sp Tutorials on PHP Some material drawn from the following good tutorials: PHP introduction and examples:  Interactive PHP with database access:  Longer PHP/MySQL Tutorial from webmonkey:  Nice insert/update/delete example from webmonkey:  MySQL/Perl/PHP page from U-Wash: 

M.P. Johnson, DBMS, Stern/NYU, Sp Comparison of scripting languages PHP v. Perl:  PHP v. Perl v. Java servlets v. …:  -side-scripting-language/ -side-scripting-language/

M.P. Johnson, DBMS, Stern/NYU, Sp Advice for use of novel languages 1. Rerun often  don’t wait until end to try 2. Use frequent prints to be sure of var vals 3. When stuck, picture continuum from your current program to some other program  other prog. works but doesn’t do what you want  change either/both, step by step, until they meet in the middle

M.P. Johnson, DBMS, Stern/NYU, Sp That’s really all, folks! Q: Is this enough to get a job coding PHP? A: Again, probably not. But: again pretty easy to produce a semi-interested site with a few copies of lookup.php and cia.php. Don’t like PHP either? Lots of other choices, but again, you’re strongly discouraged from using something else for your project unless you know what you’re doing.