CS 377 Database Systems Database Programming in PHP Li Xiong Department of Mathematics and Computer Science Emory University
Outline A Simple PHP Example Overview of Basic Features of PHP Overview of PHP Database Programming
Web Database Programming Using PHP PHP (PHP: Hypertext Preprocessor) is a open source server-side scripting language for producing dynamic web pages PHP allows access to a database and execution of calculations and logic A PHP-enabled web server interprets the PHP code and dynamically construct the web page
Typical PHP architecture
Building a web application
LAMP LAMP (Linux, Apache, MySQL, Perl/PHP/Python)
A Hello World Example in PHP <?php // prints hello world echo "Hello World!";?> <?php?> PHP start tag PHP end tag Comments: // or /* */
PHP programming environment Place your PHP source files in your cs377/web directory /home/userid/cs377/web/helloworld.php The file can be accessed at http://cs377.mathcs.emory.edu/~userid/helloworld.php
PHP Variables, Data Types, and Programming Constructs PHP variable names Start with $ symbol Can include characters, letters, and underscore character (_) Strings and text Single-quoted strings - Literal strings that contain no PHP program variables Double-quoted strings Values from variables need to be interpolated into string Period (.) symbol String concatenate operator
PHP Variables, Data Types, and Programming Constructs Numeric data types Integers and floating points Programming language constructs For-loops, while-loops, and conditional if-statements Boolean expressions Comparison operators == (equal),!= (not equal), > (greater than), >= (greater than or equal), < (less than), and <= (less than or equal)
PHP Database Programming with Oracle Oracle OCI8 library provides API for accessing Oracle database PHP OCI manual http://www.php.net/manual/en/intro.oci8.php Free book from Oracle The Underground PHP and Oracle Manual"
Typical Steps for Database Programming Establish database connection Execute SQL statements Close database connection
Connect to an Oracle database oci_connect ( string $username, string $password, string $connection_string) Returns a connection identifier needed for most other database operations. Connection String for Math/CS Oracle server '//cs377.mathcs.emory.edu:1521/cs377'
Execute SQL statements oci_parse ( resource $connection, string $sql_text ) Prepares sql_text using connection and returns the statement identifier, which can be used with oci_execute() and other functions. Statement identifiers can be freed with oci_free_statement() or by setting the variable to null
Execute SQL statements bool oci_execute ( resource $statement [, int $mode] ) Executes a statement previously returned from oci_parse(). Statements like INSERT will have data committed to the database by default. For SELECT statements, query results can be fetched with functions like oci_fetch_array(). Each parsed statement may be executed multiple times
Process query results bool oci_fetch ( resource $statement ) Fetches the next row from a query into internal buffers accessible either with oci_result() oci_result ( resource $statement, mixed $field ) Returns the data from field in the current row, fetched by oci_fetch(). array oci_fetch_array ( resource $statement [, int $mode ] ) Returns an array containing the next row from a query. Each array entry corresponds to a column of the row.
Close Database Connection bool oci_close ( resource $connection ) Unsets connection
employee.php Demo
Course Project Build a JDBC application or PHP web application for a database of your own choice! Team of 1 or 2 students Tasks Design the database Define the database schema and load the (test) data Design and implement the GUI interface Design and implement the functionalities (the queries it supports) Minimum requirement Define and populate a database GUI interface that allows search conditions and displays the data Previous project examples Music collection Baseball game board Fraternity brothers database
Project Deliverables Project proposal Motivation and objectives Proposed application Plan of action Project workshop presentation/demo in class, Tuesday, 12/6 Pizza or cookies and prize Project report and source code Motivation and objectives Description of your application Discussion Potential extensions
Midterm Thursday, 10/13/2011, in class Open book, open notes, no laptops Format similar to homeworks Covered material Database concept ER model Relational model Mapping from ER to Relational model Relational algebra SQL