Some notes have been posted for the remainder of this week. Today we want to discuss: a) making server-side state programs immune to accidental reloads b) setting up and querying a database, from PHP, Perl and Python So let's start from a program we developed yesterday:

What is + ? Answer:

Press to submit your answer.

Let's copy this program in ~/apache/htdocs/thu0702/six.php This is the server side state version of the program. Access it from http://silo.cs.indiana.edu:44063/thu0702/six.php Ideas: -- somehow delete your previous answer -- keep track how many times an answer has been sent -- name your submit button and check for it in the submission -- copy your url and compare it with the past So we end up with this code:

What is + ? Answer:

Press to submit your answer.

Press to restart the game.

Now we want to do Homework Three. I start by going into /nobackup/username/mysql-5.0.22 We remember the files we developed there thus far: -bash-3.2$ ls -ld step* -rwxr-xr-x 1 dgerman faculty 63 Jun 29 15:55 step006 -rwxr-xr-x 1 dgerman faculty 103 Jun 29 16:07 step007 -rwxr-xr-x 1 dgerman faculty 349 Jun 29 16:20 step008 -bash-3.2$ cat step008 /nobackup/dgerman/mysql/bin/mysqld_safe \ --user=dgerman \ --pid-file=/nobackup/dgerman/mysql/mysqld.pid \ --log=/nobackup/dgerman/mysql/mysqld.log \ --socket=/nobackup/dgerman/mysql/mysql.sock \ --log-error=/nobackup/dgerman/mysqld-error.log \ --basedir=/nobackup/dgerman/mysql \ --datadir=/nobackup/dgerman/mysql \ --port=44089 & -bash-3.2$ If your MySQL server is not running start it. Try to see if it's running with ps -ef | grep username. If it is not running please start it: ./step008 and hit Enter. Verify that it's running with ps -ef | grep username then go in. In your /nobackup/username/mysql-5.0.22 create a file connect_asroot. pico connect_asroot then inside write: mysql --socket=/nobackup/username/mysql/mysql.sock \ --port=44yyy -u root -p Change username to your username and the port to your MySQL port also make sure there's no space after the backslash, then save and exit and make the file executable and run it. ./connect_asroot Hit Enter when prompted for password, you're in. As root (super user) you will issue three commands: create user '[username]'@'silo.cs.indiana.edu' IDENTIFIED BY '[password]'; I type edit, then copied this in and changed it, then exit, ;, Enter. mysql> create user 'lbird'@'silo.cs.indiana.edu' IDENTIFIED BY 'sp00n';ERROR 1396 (HY000): Operation CREATE USER failed for 'lbird'@'silo.cs.indiana.edu' mysql> drop user 'lbird'@'silo.cs.indiana.edu'; Query OK, 0 rows affected (0.00 sec) mysql> create user 'lbird'@'silo.cs.indiana.edu' IDENTIFIED BY 'sp00n';Query OK, 0 rows affected (0.00 sec) Next we issue this command: create database homeworkThree Finally give the user access: grant all on databaseName.* to 'username'@'silo.cs.indiana.edu'; Replace databaseName with homeworkThree and username with the one you chose. For me this is: grant all on homeworkThree.* to 'lbird'@'silo.cs.indiana.edu'; Now exit. I create a file connect_aslbird with the contents: mysql --socket=/nobackup/dgerman/mysql/mysql.sock \ --port=44089 --host=silo.cs.indiana.edu -u lbird -p Save, make executable and run it to get in. -bash-3.2$ ls -ld conn* -rw-r--r-- 1 dgerman faculty 107 Jul 2 18:39 connect_aslbird -rwxr-xr-x 1 dgerman faculty 83 Jul 2 18:17 connect_asroot -bash-3.2$ chmod +x connect_aslbird -bash-3.2$ ls -ld conn* -rwxr-xr-x 1 dgerman faculty 107 Jul 2 18:39 connect_aslbird -rwxr-xr-x 1 dgerman faculty 83 Jul 2 18:17 connect_asroot -bash-3.2$ cat connect_aslbird mysql --socket=/nobackup/dgerman/mysql/mysql.sock \ --port=44089 --host=silo.cs.indiana.edu -u lbird -p -bash-3.2$ ./connect_aslbird Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.0.22-log Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use homeworkThree Database changed mysql> show tables; Empty set (0.00 sec) mysql> Now we're as (lbird) ready to define a database. Now type edit, Enter and in the empty editor window paste this: create table enrollment ( customer varchar(25), matchid varchar(30) , tickets decimal(4,0) , primary key (customer, matchid) ) Exit, save, and when you get the -> prompt type ; followed by Enter. If you type show tables again you get enrollment. Now type edit again, and paste this command in: insert into enrollment values ('Illinois' , 'dumi' , 40), ('Illinois' , 'joba' , 120), ('Illinois' , 'kuba' , 180), ('Illinois' , 'mibi' , 140), ('Iowa' , 'bijo' , 200), ('Iowa' , 'dubi' , 120), ('Iowa' , 'kubi' , 180), ('Indiana' , 'bijo' , 150), ('Indiana' , 'joba' , 100), ('Indiana' , 'joku' , 60), ('Indiana' , 'mijo' , 210), ('Michigan' , 'duba' , 300), ('Michigan' , 'joku' , 240), ('Michigan' , 'kudu' , 220), ('Purdue' , 'dubi' , 80), ('Purdue' , 'miba' , 200), ('Purdue' , 'mibi' , 60), ('Purdue' , 'mijo' , 120), ('Minnesota', 'duba' , 80), ('Minnesota', 'dumi' , 120), ('Minnesota', 'joku' , 60), ('Minnesota', 'kudu' , 20) Exit, save, type ; at -> hit Enter. Run this query: mysql> select * from enrollment where customer = 'Purdue'; +----------+---------+---------+ | customer | matchid | tickets | +----------+---------+---------+ | Purdue | dubi | 80 | | Purdue | miba | 200 | | Purdue | mibi | 60 | | Purdue | mijo | 120 | +----------+---------+---------+ 4 rows in set (0.00 sec) If you get this result you're done. Exit. Go to ~/apache/htdocs/ and create homeworkThree.php: "; $query = "select movieNum, avg(rating) as ratings from projectRatings group by movieNum order by ratings desc "; $result = @mysql_query($query); ?>
Movie Average Rating " . $row[0] . "" . $row[1]; } ?>
"; } } else { echo "I cannot connect.

"; } ?> Use pico -w homeworkThree.php to avoid broken lines. Then adjust it: "; $query = "select * from enrollment where customer = 'Purdue'"; $result = @mysql_query($query); ?>

University Match ID Number of tickets purchased " . $row[0] . "" . $row[1] . "" . $row[2]; } ?>
"; } } else { echo "I cannot connect.

"; } ?> Then save it and call it from: http://silo.cs.indiana.edu:44063/homeworkThree.php You get what you expect, what you saw in MySQL from the command line. --