| |||||
PHP NotesIndex
Setting up development environmentPHP5 should be already be installed and running on the server. It runs with the Apache2 web server, which you can access at http://localhost/ using a browser on the server. An example PHP5 application can be found in /home/dbcourse/examples/php-db-beers/. This directory contains the source code for a simple db-beers web application, which we will use throughout the rest of this document as an example of developing and deploying PHP5 applications. Make a copy of it in your home directory, using the command "cp -r /home/dbcourse/examples/php-db-beers/ ~/". You will also need to set up the beer drinkers' database tables (if you have not already), by running "/home/dbcourse/examples/db-beers/setup.sh". The /home/dbcourse/etc/pdo-datasource.php stores information necessary for your PHP5 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 ~/php-db-beers/src/. Developing and deploying sitesGo into the directory ~/php-db-beers/. The src/ subdirectory contains the PHP files and other things that you can "deploy" onto the web by running "./deploy.sh". The script bascially copies the src/ directory to /var/www/db-beers/, so that you can access the application using the URL http://localhost/db-beers/. You can modify deploy.sh and change db-beers to something else for your own application. A typical development cycle consists of coding/debugging -> ./deploy.sh -> testing on browser, and then back to coding/debugging. For debugging, you will find the detailed Apache log in /var/log/apache2/error.log useful. A mini-tutorial of PHP/PDOThe concept behind PHP5 (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 PHP5 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. Additional information |
|||||
Last updated Sun Sep 11 21:40:21 EDT 2011 |