Debugging Hints for Project 2

Testing One Piece at a Time

Your code is likely a mix of SQL queries and PHP code. When things go wrong it can be due to many reasons, like your MySQL server is not running, your table has not been loaded correctly, your SQL query does not return what you expect it to return, and your PHP code has a typo or is incorrect. You will need to look at each of these possibilities, one at a time, making sure that each component is working as expected. It is often helpful to carefully look at the displayed “error message,” because it may give you a strong hint on the most likely source of your error.

In general, we recommend

  1. First investigate your tables via mysql terminal to ensure that they have been loaded fine. In particular, if your code fails all tests in the autograder it is a good idea to look into your load.sql. It should work if there is no table in the database.
  2. Issue the SQL commands used in your PHP code directly inside the mysql terminal to ensure that their results are what you expect and
  3. Take out most of your PHP code and reintroduce them piece by piece to see where the error might be coming from.

In summary, the best way to debug “why isn’t something showing” is to separate out the components.

Displaying Detailed Error Messages

When something goes wrong in your code, it is incredibly helpful to be able to see the detailed error message. Introduce the following sets of lines at the top of your PHP page for printing verbose errors on the file that encountered problems (credit to Brandon Hsu):

<?php
// report all PHP errors
error_reporting(E_ALL);

// display error messages in the output page
ini_set("display_errors", "1");

// log error messages in /tmp/php-error.log
ini_set("log_errors", "1");
ini_set("error_log", "/tmp/php-error.log");
?>

Once set, you will be able to see detailed error messages both in the returned page and in the file /tmp/php-error.log in the container. In particular, the displayed error messages include stack traces and memory errors, which can be very helpful for identifying the source of your error. In the past, students often had an error regarding the order of $rs->free(); and $db->close();

Apache Error Log

The above config will display PHP errors, but sometimes it may be useful to see the error logs produced by the Apache Web server as well. This can be done using the following command:

sudo tail /var/log/apache2/error.log

(password: password). tail can be of course swapped with any other utility for viewing the log output. Be careful with the sudo command, though, because you can damage the container irreparably if not careful.

Sending Repeated Requests

In some cases, a bug in your code shows up only after many requests have been sent to your server. To check for this case, you will need to repeatedly send HTTP requests to the server in a short span of time. Doing this manually using a browser is tedious, so we recommend using the wget command like the following:

wget http://localhost:8888/movie.php?id=2632

Factory Reset of Container

It sometimes happens that your container is damaged and it is easier to “start fresh” by “factory resetting” your container. First, make sure that you save your work so far by copying them over to your host machine. Then delete the existing container and start fresh by running the following commands in your host computer:

docker container rm mysql-apache
docker run -it -v {your_shared_dir}:/home/cs143/shared -p 8888:80 --name mysql-apache junghoo/mysql-apache 

Search and/or Ask on Piazza/Campuswire

If nothing works, it is always a good idea to search on Piazza (and Google) with the error messages that you see. More likely than not, others have experienced and dealt with issues similar to yours. If not, you can ask for other students and course staff’s help on Piazza. When you post a question on Piazza, make sure that you do not share more than 3 explicit lines of your code. Sharing your code publicly is a violation of our honor code.