M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #17 M.P. Johnson Stern School of Business, NYU Spring, 2008
M.P. Johnson, DBMS, Stern/NYU, Spring 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.?
M.P. Johnson, DBMS, Stern/NYU, Spring PHP-like scripting Program Client Server HTTP Request Data for program Generated HTML HTML Image from
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring Form example On clicking Send, we go to the same page, but with “name=99&sumbit=OK” Enter a number: Enter a number:
M.P. Johnson, DBMS, Stern/NYU, Spring PHP and forms Obtain param a param, just prefix with $ (for now..) Goal: display text and button; On submit, tell user what was entered Improve: also print, say, triple the input… if (isset($val)) print "You entered $val! "; if (isset($val)) print "You entered $val! ";
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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.
M.P. Johnson, DBMS, Stern/NYU, Spring 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, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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 $ wget $ emacs hello.php $ pico hello.php $ cd public_html $ wget $ wget $ emacs hello.php $ pico hello.php
M.P. Johnson, DBMS, Stern/NYU, Spring Limit: PHP webpages that do something Semi-interesting PHP scripts: 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, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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, Spring Insert/delete PHP example Similar to search example NB: form has two buttons
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring All 1. Run/read these PHP scripts: 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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;
M.P. Johnson, DBMS, Stern/NYU, Spring 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: Example currently here:
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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;
M.P. Johnson, DBMS, Stern/NYU, Spring 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;
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring 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
M.P. Johnson, DBMS, Stern/NYU, Spring Etc?