| ||||||||||||||||||
| ||||||||||||||||||
| ||||||||||||||||||
PHP NotesIndexNotationIn the following, HOSTNAME refers to the fully qualified host name of your machine (e.g., dbcourse-junyang.cloudapp.net). USER refers to your login name on this machine (e.g., azureuser). Setting upTo install PHP 5 on your machine and to set up an example site, run the command "/opt/dbcourse/install/php5/setup.sh". This command will create the directory /var/www/db-beers/, which houses the pages for the site, which we will use throughout the rest of this document as an example. Once installed, PHP runs with the Apache2 web server, which you can access at the URL http://HOSTNAME/ using a browser. The example site can be accessed at the URL http://HOSTNAME/db-beers/. Working on the siteJust 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://HOSTNAME/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 USER:USER /var/www/app_name/") so you can create and edit files in it. A mini-tutorial of PHP/PDOThe 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 /etc/php5/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 |
||||||||||||||||||
Last updated Wed Sep 11 10:18:08 EDT 2013 |