Download presentation
Presentation is loading. Please wait.
1
M.P. Johnson, DBMS, Stern/NYU, Spring 20061 C20.0046: Database Management Systems Lecture #16 M.P. Johnson Stern School of Business, NYU Spring, 2008
2
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 2 Homework Project part 2 due Wed in class/by midnight Project part 3 posted soon… Topic: populating your tables with data Later: Project part 5 Front-end
3
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 3 Agenda: Programming for SQL Have now been exposed to: Embedded SQL: Pro*C Java JDBC All used; good to know about Most important for this course: DB-conn from web scripting languages PHP
4
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 4 Goals: by next week Today: be able to post a hello-web PHP script in your i5 account Next week: Be able to write simple dynamic webpages in In PHP that 1. Take input from user 2. Execute SQL query 3. Display formatted results Based on examples from class…
5
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 5 New topic: web apps Goal: web front-end to database Present dynamic content, on demand Not canned (static) pages/not canned queries (perhaps) modify DB on demand Naïve soln: static webpage & HTTP index.html written, stored, put on server, displayed when it’s url is requested HTTP is stateless (so?) This doesn’t solve our problem
6
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 6 Dynamic webpages Soln 1: upon url request 1. somehow decide to dynamically generate an html page (from scratch) 2. send back new html page to user No html file exists on server, just created on demand CGI/Perl, Java servlets, etc.
7
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 7 New topic: CGI First, and still very popular method CGI: Common Gateway Interface Not a programming language! Just an interface (connection) between the webserver and an outside program “Webserver” = webserver software, e.g., Apache Very simple basic idea: 1. user chooses an url 2. webserver runs that url’s program, 3. sends back the program’s output
8
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 8 On-the-fly content with CGI Program Client Server HTTP Request Data for program Generated HTML HTML Image from http://www.scit.wlv.ac.uk/~jphb/cp3024/
9
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 9 Using CGI CGI works with any prog./scripting lang. Really? Well, if things are set up right…
10
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 10 CGI works… if the webserver machine can run program pages/soho, not sales and if the user the webserver is running as (e.g. nobody) can can run your program and if the necessary jars/libraries are available and if nobody has permission to use them and if the necessary DB software is installed Plausible choices: Perl, Python, C, sh
11
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 11 CGI admin Most webservers: CGI program/script must either 1. End in.cgi and/or 2. Reside in cgi-bin If an actual program, the cgi file is just the name of the executable: In a script, first (“shebang”) line says which interpreter to use: gcc -o myprog.cgi myproc.gcc #!/usr/local/bin/perl
12
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 12 CGI input CGI programs must respond to input, two ways GET: string is part of the URL, following a ?: POST: string can be read by program from an environmental variable Vars not visible to the browser user Not automatically put in server log, etc. http://google.com
13
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 13 CGI summary One big advantage of CGI: not a language Existing command-line programs can be called from the web Web user can enter info Program output sent back as webpage Don’t want to start a new process for every user/pageview/roundtrip of your site?
14
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 14 PHP-like scripting Program Client Server HTTP Request Data for program Generated HTML HTML Image from http://www.scit.wlv.ac.uk/~jphb/cp3024/
15
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 15 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/fill in the file Send back the modified html page to user An incomplete html page exists on server Examples: PHP, JSPs, ASPs, etc.
16
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 16 Review: dynamic webpages First option: for each request: run program, produce whole page, send back CGI & some host language, Java Servlets, etc. 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 – recursion
17
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 17 Getting started Must set permissions in account on i5 Unix commands: ls, cd, pwn, cp, mv chmod command Owner/group/all Read/write/execute Bitstrings…
18
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 18 hello.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/hello.php Q: What the difference between and \n? Hello from PHP Here comes the PHP part: \n"; ?> That's it! Hello from PHP Here comes the PHP part: \n"; ?> That's it!
19
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 19 Script errors Script errors, w/ and w/o display_errors on: http://pages.stern.nyu.edu/~mjohnson/dbms/perl/hello.php http://pages.stern.nyu.edu/~mjohnson/dbms/perl/hello.php Local dir must contain.htaccess: Automatically load GET/POST params as vars http://pages.stern.nyu.edu/~mjohnson/dbms/php/.htaccess http://pages.stern.nyu.edu/~mjohnson/dbms/php/.htaccess cp /home1/m/mpj3/public_html/php/.htaccess. php_flag display_errors on php_flag register_globals on php_flag display_errors on php_flag register_globals on
20
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 20 PHP vars Names always start with $ http://pages.stern.nyu.edu/~mjohnson/dbms/php/math.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/math.php <? $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. " "; ?>
21
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 21 Combining PHP and HTML http://pages.stern.nyu.edu/~mjohnson/dbms/php/combine.php <?php for($z=0;$z<=5;$z++) { ?> Iteration number <? } ?> <?php for($z=0;$z<=5;$z++) { ?> Iteration number <? } ?>
22
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 22 More PHP syntax Somewhat C-like, somewhat Perl-like Case-sensitive Strings: Concatenation op:. Single, double quotes similar to Perl Comments: # Unix shell-style /* */ C-style // C++-style Output: echo(“hi there”); print(“hi there”); C’s printf
23
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 23 PHP info PHP does not have both string and number ops like Perl Number ops treat (number) strings as numbers, regular strings as strings http://pages.stern.nyu.edu/~mjohnson/dbms/php/test.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/test.php Info function displays lots of server info: http://pages.stern.nyu.edu/~mjohnson/dbms/php/info.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/info.php
24
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 24 New topic: HTML forms Interactive parts of HTML: forms Intuition for name: paper form Fill in textboxes, check boxes or not, etc. Turn it in (press button) HTML form contains arb. # of INPUTs Submits to somewhere (ACTION) By GET or POST
25
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 25 Form example On clicking Send, we go to the same page, but with “name=99&sumbit=OK” http://pages.stern.nyu.edu/~mjohnson/dbms/php/input.php Enter a number: Enter a number:
26
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 26 PHP and forms Obtain param a param, just prefix with $ (for now..) Goal: display text and button; On submit, tell user what was entered http://pages.stern.nyu.edu/~mjohnson/dbms/php/input.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/inputphp.txt Improve: also print, say, triple the input… if (isset($val)) print "You entered $val! "; if (isset($val)) print "You entered $val! ";
27
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 27 PHP error-handling Many PHP scripts have lines of the form some-statement OR die(“something happened”); What this means: die exits with error message PHP uses OR as and AND for bool operators PHP 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
28
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 28 New topic: PHP and databases PHP 5 has a JDBC-style DB interface But we’re using PHP 4.3.4… Special-purpose methods/libraries for MySQL, etc. Use these to obtain a connection, prepare and execute queries, etc.
29
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 29 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);
30
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 30 PHP & MySQL 3. Extract next row of data from the results: What this means: myrow is an array that can then be accessed Other options, see code In general, to scroll through results, do: $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
31
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 31 Limit: PHP webpages that do something Semi-interesting PHP scripts: http://pages.stern.nyu.edu/~mjohnson/dbms/php/lookupphp.txt http://pages.stern.nyu.edu/~mjohnson/dbms/php/lookupphp.txt http://pages.stern.nyu.edu/~mjohnson/dbms/php/lookup.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/lookup.php 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!
32
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 32 lookup.php 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
33
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 33 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
34
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 34 Insert/delete PHP example Similar to search example NB: form has two buttons http://pages.stern.nyu.edu/~mjohnson/dbms/php/update.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/updatephp.txt
35
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 35 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 http://pages.stern.nyu.edu/~mjohnson/dbms/php/cia.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/ciaphp.txt
36
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 36 That’s all, folks! Q: Is this enough to get a job coding PHP? A: Probably not! But: a couple modified copies of lookup.php and/or cia.php + some HTML glue fairly interesting site a couple modified copies of lookup.php and/or cia.php + some HTML glue fairly interesting site
37
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 37 For next time (in lab)… 1. Run/read these PHP scripts: http://pages.stern.nyu.edu/~mjohnson/dbms/php/lookup.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/lookup.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/lookupphp.txt http://pages.stern.nyu.edu/~mjohnson/dbms/php/lookupphp.txt http://pages.stern.nyu.edu/~mjohnson/dbms/php/update.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/update.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/updatephp.txt http://pages.stern.nyu.edu/~mjohnson/dbms/php/updatephp.txt http://pages.stern.nyu.edu/~mjohnson/dbms/php/cia.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/cia.php http://pages.stern.nyu.edu/~mjohnson/dbms/php/ciaphp.txt http://pages.stern.nyu.edu/~mjohnson/dbms/php/ciaphp.txt 2.Go through at least one tutorial on PHP (on web/below) 3.Try posting a hello-web PHP script in your sales account Various others in dbms/php…dbms/php
38
M.P. Johnson, DBMS, Stern/NYU, Spring 2006 38 Tutorials on PHP Some material drawn from the following good tutorials: http://php.net PHP introduction and examples: http://www.scit.wlv.ac.uk/~jphb/sst/php/ http://www.scit.wlv.ac.uk/~jphb/sst/php/ Interactive PHP with database access: http://www.scit.wlv.ac.uk/~jphb/sst/php/gazdb.html http://www.scit.wlv.ac.uk/~jphb/sst/php/gazdb.html Longer PHP/MySQL Tutorial from webmonkey: http://hotwired.lycos.com/webmonkey/99/21/index2a.html http://hotwired.lycos.com/webmonkey/99/21/index2a.html Nice insert/update/delete example from webmonkey: http://hotwired.lycos.com/webmonkey/99/21/index3a.html http://hotwired.lycos.com/webmonkey/99/21/index3a.html MySQL/Perl/PHP page from U-Wash: http://www.washington.edu/computing/web/publishing/mysql-script.html http://www.washington.edu/computing/web/publishing/mysql-script.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.