User's Manual

Connecting to the Database
3-4 Oracle Database Express Edition 2 Day Plus PHP Developer Guide Beta Draft
Connecting to the Database
1. To form a database connection in your PHP application, you use the
oci_connect() function with three string parameters:
$conn = oci_connect($username, $password, $db)
The first and second parameters are the database username and password,
respectively. The third parameter is the database connection identifier. The
oci_connect() function returns a connection resource needed for other OCI8
calls, otherwise it returns FALSE if an error occurs. The connection identifier
return is stored in a variable called $conn.
To validate that the oci_connect() call returns a usable database connection,
write a do_query() function that accepts two parameters: the database
connection identifier, obtained from the call to oci_connect(), and a query
string to select all the rows from the DEPARTMENTS table.
Edit anyco.php to form a database connection with the following parameter
values:
Q Username is hr.
Q Password for this example is hr. Remember to use the actual password of
your HR user.
Q Oracle connect identifier is //localhost/XE.
The file becomes:
<?php // File: anyco.php
require('anyco_ui.inc');
// Create a database connection
$conn = oci_connect('hr', 'hr', '//localhost/XE');
ui_print_header('Departments');
do_query($conn, 'SELECT * FROM DEPARTMENTS');
ui_print_footer(date('Y-m-d H:i:s'));
// Execute query and display results
function do_query($conn, $query)
{
$stid = oci_parse($conn, $query);
$r = oci_execute($stid, OCI_DEFAULT);
print '<table border="1">';
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
print '<tr>';
foreach ($row as $item) {
print '<td>'.
($item ? htmlentities($item) : '&nbsp;').'</td>';
}
print '</tr>';
}
print '</table>';
}
?>