|
Due Thursday November 19 |
Even if you haven't used PHP before (and we're assuming you haven't), it may be possible to complete the basic project with minimal help from additional PHP materials. However, if you like to read tutorial materials before jumping in, or if you're feeling confused about PHP, we recommend this PHP MySQL Introduction. This reference, and many others you may find useful, are linked to the class Support Materials page.
Activation can take up to 24 hours. We hope you completed this
step when it was first suggested in the Project Part 2 specification. Visit Activate
Personal CGI Service and follow the instructions to activate your
CGI account. Once activated, you will have a new directory
~/cgi-bin/.
Copy the sample PHP code we are providing into your cgi-bin
directory:
cp /usr/class/cs145/pa3sample/* ~/cgi-bin/.
In your cgi-bin directory you first need to configure
the mysqlvars.php file with your MySQL username,
password, and database name. Then, for a first sample of how PHP pages
work, visit the URL:
http://www.stanford.edu/~[yourname]/cgi-bin/currtime.phpwhere
[yourname] is your SUNet ID. At this URL you should
see the current time as reflected in your Time table. If
this code doesn't work, you may need to edit currtime.php
so that it uses your Time table (named Time in the
sample) and the correct attribute within your Time table (named
currenttime in the sample -- note there are two
references).
At this point, your currtime.php should be working.
File currtime.php gives an example of how to use PHP
to connect to MySQL, execute commands, and process the results. A
second sample file, selecttime.php, is designed to help
you get familiar with input boxes. (File kill.php will be
discussed later.) Examine the sample code, and feel free to adapt this
code directly for your project. If you're having trouble understanding
the sample code, we recommend the PHP MySQL
Introduction mentioned earlier.
currtime.php connects to MySQL using
mysqli_connect, as opposed to
mysql_connect. We have chosen to use MySQLi ("MySQL
Improved Extension") for its improved support of transactions. For
more on how to use MySQLi with transactions, please see MySQLi
and Transactions, as well as the section on Transactions,
errors, and constraint-checking below.
currtime.php does not need to use a loop. For an example of how to loop through a set of result tuples, please see PHP MySQL Select from the tutorial.
selecttime.php uses the $_POST variable. This variable is used to collect values from an HTML form with method="post". Please see PHP POST for more details.
Full credit also requires general error- and constraint-checking as specified in the Transactions, errors, and constraint-checking section of this document, below. All of your required constraints from Part 2 of the project should be enforced in your "live" AuctionBase system.
Here are some examples of additional capabilities beyond our basic expectations that you might consider adding to your system if you're feeling ambitious. Use your imagination to come up with even more.
htmlspecialchars useful).If it helps, you may assume that AuctionBase has only one user operating on it at a time. Although transactions may be useful for database modifications and constraint-checking, you do not need to worry about transactions as a concurrency-control mechanism. That said, even without special effort your system may turn out to be fairly robust for multiple users.
Reminder: You must create your tables using the "InnoDB" storage engine for constraint-checking and transactions to work properly.
Here is an example of how you might structure code that involves database modifications. <?php
// kill script after 20 seconds
set_time_limit(20);
// function to handle errors: rollback and exit
function error_exit ($con, $errormsg="unspecified"){
echo "We're sorry: a system error occurred.<p></p>";
echo "Error Message: " . $errormsg;
mysqli_rollback($con); // if error, roll back transaction
mysqli_close($con);
exit();
}
// establish MySQL connection
$link = mysqli_connect($host,$username,$password,$dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// step 1: turn off auto-commit
$result = mysqli_autocommit($link, FALSE);
if (!$result) {
error_exit($link, "autocommit"); // if error, roll back transaction
}
// step 2: perform action
// run command 1
$com1 = "select/insert/delete/update ... ";
$result = mysqli_query($link, $com1);
if (!$result) {
error_exit($link, "com1"); // if error, roll back transaction
}
// run command 2
$com2 = "select/insert/delete/update ... ";
$result = mysqli_query($link, $com2);
if (!$result) {
error_exit($link, "com2"); // if error, roll back transaction
}
// step 3: assuming no errors, commit transaction
$result = mysqli_commit($link);
if (!$result) {
error_exit($link, "commit"); // if error, roll back transaction
}
echo "Success";
// close connection
mysqli_close($link);
?>
Here's some explanation of the above code:
set_time_limit(20) prevents this script from
running longer than 20 seconds (excluding time spent running tasks
such as database queries). Placing this command at the top of your PHP
scripts will help prevent your system from leaving open a large number
of MySQL connections. Note in particular that closing the browser
after visiting a PHP script does not stop the script's
execution, so leaving connections open can occur easily without this
time limit.
error_exit function, declared next,
is called whenever an error occurs. It prints an error message, then
rolls back the current transaction and exits.
mysqli_connect to open a MySQL
connection, then we turn off "autocommit" using
mysqli_autocommit($link, FALSE). Doing so commits any
currently uncommmited commands, then starts a new transaction.
select/insert/delete/update commands. In the above code,
we have two commands, $com1 and $com2. We
first call mysqli_query with the command
$com1. A return value of FALSE indicates
that an error occurred. (This is the point at which a
constraint violation or trigger error is caught; other errors are possible
here as well.) If an error has occurred, we call
error_exit. Executing $com2 is similar.
mysqli_commit, then close the MySQL connection
using mysqli_close. Although connections are
closed automatically at the end of PHP scripts, it is still good
practice to close connections explicitly. Myth
machines. If there is a compelling reason you cannot make your system
work in the Myth browser environment (e.g., you really
want to exploit certain features in Internet Explorer), you must get
"preapproval" from the course staff to use a different browser
environment. Send an email message to cs145@cs.stanford.edu telling
us precisely what browser environment you wish to use for your
project. The message must be sent by Friday November 13 so that we
have time to work things out if your browser environment poses a
problem for us. You will receive a reply within 24 hours of your
message, and you must receive a positive confirmation message before
assuming that your alternate environment is okay.
When the preapproval process is not followed, projects that have
problems on the Myth Firefox browsers may lose
points, possibly all points if we cannot run your project at all.
Ctrl-C. Once you are certain your queries are working
properly, incorporate them into your web interface.
If you notice an unexplained slowdown in system performance, it may be due to a large number of open MySQL connections. Here are some tips to avoid this problem:
Ctrl-Z does not kill queries or close
connections in the MySQL command-line interface; it only suspends the
client. Queries should be killed using Ctrl-C.
Ctrl-Z, follow up afterwards
with "ps aux | grep [username]" to find your process ID,
then "kill [processID]" to kill the process. Otherwise
the MySQL connection remains open.
set_time_limit(20) at the top
of your PHP scripts to prevent MySQL connections from remaining open
indefinitely. Note again that closing the browser after visiting a
PHP script does not stop the script's execution.
show
processlist" shows the list of MySQL processes, and a process
can be killed with "kill [Id]". Alternatively, to kill
all open connections, visit the page kill.php, which we
provided with the sample code.
Most importantly: It is imperative that we can run your
project with a minimum of effort on the Myth
machines. Due to the size of the class we will not be able to set up
separate environments for individual projects, or conduct private
demos.
README.txt, along with all of the files necessary to make
your website run correctly when copied into the grader's cgi-bin
directory.
Your README.txt file should include at least
the following, in this order:
I WOULD LIKE TO ENTER THE AUCTIONBASE
CONTEST" if you want your project to be considered for the
contest described below. Otherwise leave blank.Since we will be connecting to your MySQL database through your web application, make sure that before submission and throughout the grading period your database is loaded with all of the appropriate data, constraints, and triggers.
If you did not use PHP: It's imperative that we can run your project with a minimum of effort on the Myth machines. You must submit detailed instructions and all of your source code.
As usual, from your submission directory execute the script:
/usr/class/cs145/bin/submit-projectYou may resubmit as many times as you like, however only the latest submission and timestamp are saved, and those are what we will use for grading your work and determining late penalties. Submissions via email will not be accepted.
Remember that points may be deducted if you do not follow the submission procedures exactly as specified.
|
|
We will select a small number of AuctionBase systems as winners of our annual CS145 AuctionBase Contest. Winners of the contest will:
Important - If you want your project to be considered for
the contest, you must clearly indicate so at the top of your
README file, as described in What to submit
above.