CompSci 316 (Fall 2012):
Introduction to Database Systems

Course Information
Lecture Notes
Assignments
Tentative Syllabus
Programming Notes
Getting Started with the dbcourse Virtual Machine
Getting Started with Gradiance
PostgreSQL Notes
Using ra
JDBC Notes
Tomcat Notes
Django Notes
PHP Notes
XML Notes
Homework/Project Submission
Sakai (Grades/Project Discussion)

PHP Notes


Index


Setting up the site

PHP5 should be already be installed and running. It runs with the Apache2 web server, which you can access at the URL http://php.my.net/ using a browser.

As an example, we have created a simple db-beers web application, which we will use throughout the rest of this document. To set it up, run "/home/dbcourse/examples/php-db-beers/setup.sh". This command will copy all application code and pages to the directory /var/www/db-beers/. You access the site at the URL http://php.my.net/db-beers/.

This application works with the beer drinkers' database, which has already been set up for you. If you ever need to "reset" the database content (after you modified it, for example), just run "/home/dbcourse/examples/db-beers/setup.sh".


Working on the site

Just go into the directory /var/www/db-beers/ and modify the files there. The website immediately reflects your changes (you might need to reload the page in your web browser).

For debugging, you will find the detailed Apache log in /var/log/apache2/error.log useful.

If you want to create another application that can be accessed by URL http://php.my.net/app_name/, just make a new directory under /var/www/ ("sudo mkdir /var/www/app_name/") and give its ownership to your user ("sudo chown -R ubuntu:ubuntu /var/www/app_name/") so you can create and edit files in it.


A mini-tutorial of PHP/PDO

The concept behind PHP (or any other server-side dynamic page generation language) is very simple: Instead of serving a static HTML page, the web server executes a piece of code to generate the HTML output to serve to the client. Such pieces of code are enclosed within <?php ... ?>.

The syntax of PHP resembles Java and C++, so it should be fairly easy to pick up. You can start with a simple tutorial (link), or you can dive into the code in the src/ directory, which is fairly well documented.

PDO (link) is a standard PHP extension that allows PHP code to work with a database server. Note that statement and connection objects will be automatically closed when a page finishes, so the code is simple. Note the use of exceptions to catch database errors.

The file /home/dbcourse/etc/pdo-datasource.php stores information necessary for your PHP application to connect to the local PostgreSQL database via PDO (link). This file should be included by any .php file that needs to connect to the database. Examples of dealing with databases via PDO can be found in /var/www/db-beers/.


Additional information

  • PHP5 documentation (link), including tutorial (link) and PDO (link)
Last updated Wed Sep 12 21:58:01 EDT 2012