Download presentation
Presentation is loading. Please wait.
1
M.P. Johnson, DBMS, Stern/NYU, Spring 20081 C20.0046: Database Management Systems Lecture #17 M.P. Johnson Stern School of Business, NYU Spring, 2008
2
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 2 Agenda Project part 2 due today Project part 3 posted soon (Thurs?)… Topic: populating your tables with data Today: Html/php/mysql Bulk loader Transactions? Etc.?
3
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 3 PHP-like scripting Program Client Server HTTP Request Data for program Generated HTML HTML Image from http://www.scit.wlv.ac.uk/~jphb/cp3024/
4
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 4 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
5
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 5 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:
6
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 6 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! ";
7
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 7 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
8
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 8 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.
9
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 9 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);
10
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 10 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
11
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 11 Obtaining my examples After logging into sales: Or getting examples directly from the web: $ cd public_html $ cp ~mjohnson/public_html/dbms/php/.htaccess. $ cp ~mjohnson/public_html/dbms/php/hello.php. $ emacs hello.php $ pico hello.php $ cd public_html $ cp ~mjohnson/public_html/dbms/php/.htaccess. $ cp ~mjohnson/public_html/dbms/php/hello.php. $ emacs hello.php $ pico hello.php $ cd public_html $ wget http://pages.stern.nyu.edu/~mjohnson/dbms/php/hello.php $ wget http://pages.stern.nyu.edu/~mjohnson/dbms/php/.htaccess $ emacs hello.php $ pico hello.php $ cd public_html $ wget http://pages.stern.nyu.edu/~mjohnson/dbms/php/hello.php $ wget http://pages.stern.nyu.edu/~mjohnson/dbms/php/.htaccess $ emacs hello.php $ pico hello.php
12
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 12 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!
13
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 13 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
14
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 14 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
15
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 15 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
16
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 16 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
17
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 17 All 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
18
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 18 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
19
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 19 Advice for use of novel languages 1. Rerun often Don’t write the whole thing and then try to run 2. Use frequent prints to be sure of var vals (While debugging) 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 4. Google is your friend Search for error messages, situations
20
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 20 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
21
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 21 New topic: the bulk loader To insert data, can insert rows one at a time with INSERT INTO VALUES(<>) If data is in/can be computed from other tables, can use INSERT INTO SELECT … Often, have text file of data MySQL’s bulk loader will parse file and insert all into the database
22
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 22 Running the bulk loader The bulk loader is a command-line program mysqlimport, separate from SQL*Plus: At cmd line, specify: user/pass (pass is optional here) Host Database Input file / table name i5% mysqlimport -umy-NetID -p --local my-db imptest.txt
23
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 23 IMPORT DATA command Can also load data while inside mysql: Does not work without LOCAL Means: reading from client (your directory), not server mysql> LOAD DATA LOCAL INFILE 'imptest.txt' INTO TABLE imptest; http://pages.stern.nyu.edu/~mjohnson/dbms/proj4.html
24
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 24 For more info & example If you want, you can Use a different field separator Reorder the fields Import date values For more info, see the webpages: http://dev.mysql.com/doc/mysql/en/load-data.html http://dev.mysql.com/doc/mysql/en/load-data.html http://dev.mysql.com/doc/mysql/en/mysqlimport.html http://dev.mysql.com/doc/mysql/en/mysqlimport.html Example currently here: http://pages.stern.nyu.edu/~mjohnson/dbms/proj406.html http://pages.stern.nyu.edu/~mjohnson/dbms/proj406.html http://pages.stern.nyu.edu/~mjohnson/dbms/imptest.txt http://pages.stern.nyu.edu/~mjohnson/dbms/imptest.txt
25
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 25 For rest tonight: group quiz (not graded) 1. Import impdata.txt into a table in your account Submit a copy of select * from impdata 2. Post a (working) Hello, PHP file to your account Submit url 3. Post and modify (in some interesting way) one of my other sample PHP pages in /php to your account/php Submit url and an explanation of what you changed
26
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 26 New-old topic: Transactions So far, have simply issued commands Ignored xacts Recall, though: an xact is an operation/set of ops executed atomically In one instant ACID test: Xacts are atomic Each xact (not each statement) must leave the DB consistent
27
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 27 Default xact behavior (in Oracle) An xact begins upon login By default, xact lasts until logoff Except for DDL statements They automatically commit Examples with two views of tbl… But with TYPE=innodb ! mysql> set autocommit = 0
28
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 28 Direct xact instructions At any point, may explicitly COMMIT: SQL> COMMIT; Saves all statements entered up to now Begins new xact Conversely, can ROLLBACK SQL> ROLLBACK; Cancels all statements entered since start of xact Example: delete from emp; or delete junk;
29
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 29 Direct xact instructions Remember, DDL statements are auto- committed They cannot be rollbacked Examples: Q: Why doesn’t rollback “work”? drop table junk; rollback; drop table junk; rollback; truncate table junk; rollback; truncate table junk; rollback;
30
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 30 Savepoints (in Oracle?) Xacts are atomic Can rollback to beginning of current xact But might want to rollback only part way Make 10 changes, make one bad change Want to: roll back to before last change Don’t have Word-like multiple undo But do have savepoints
31
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 31 Savepoints Create a savepoint: emp example: --changes SAVEPOINT sp1; --changes SAVEPOINT sp2; --changes SAVEPOINT sp3 --changes ROLLBACK TO SAVEPOINT sp2; ROLLBACK TO SAVEPOINT sp1; --changes SAVEPOINT sp1; --changes SAVEPOINT sp2; --changes SAVEPOINT sp3 --changes ROLLBACK TO SAVEPOINT sp2; ROLLBACK TO SAVEPOINT sp1; SAVEPOINT savept_name; Can skip savepoints But can ROLLBACK only backwards Can ROLLBACK only to last COMMIT
32
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 32 AUTOCOMMIT (in Oracle?) Finally, can turn AUTOCOMMIT on: SQL> SET AUTOCOMMIT ON; Can put this in your config file Can specify through JDBC, etc. Then each statement is auto-committed as its own xact Not just DDL statements
33
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 33 Etc?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.