User's Manual
Beta Draft Querying Data 4-1
4
Querying Data
In this chapter you extend the Anyco HR application from chapter 3 by adding
additional information to the departments form. You also implement the functionality
to query, insert, update, and delete employees in a specific department.
This chapter has the following topics:
Q Centralizing the Database Application Logic
Q Writing Queries with Bind Variables
Q Navigating Through Database Records
Q Extending the Basic Departments Form
Q Building the Basic Employee Form
Centralizing the Database Application Logic
Modify your application code by moving the database access logic into separate files
for inclusion in the PHP application. Create new files in the
$HOME/public_html/chap4 directory.
1. Copy the files completed in chapter 3, to a new chap4 directory:
mkdir $HOME/public_html/chap4
cp $HOME/public_html/chap3/* $HOME/public_html/chap4
cd $HOME/public_html/chap4
2. Using your preferred editor, create a file called anyco_cn.inc, which defines
named constants for the database connection information. This file enables to
change connection information in one place.
<?php // File: anyco_cn.inc
define('ORA_CON_UN', 'hr'); // Username
define('ORA_CON_PW', 'hr'); // Password
define('ORA_CON_DB', '//localhost/XE'); // Connection identifier
?>
3. Create a file called anyco_db.inc that declares functions for creating a database
connection, executing a query, and disconnecting from the database. Use the
following logic, which includes some error handling that is managed by calling an
additional function called db_error ():
<?php // File: anyco_db.inc