User's Manual

Centralizing the Database Application Logic
4-2 Oracle Database Express Edition 2 Day Plus PHP Developer Guide Beta Draft
function db_connect()
{
// use constants defined in anyco_cn.inc
$conn = oci_connect(ORA_CON_UN, ORA_CON_PW, ORA_CON_DB);
if (!$conn) {
db_error(null, __FILE__, __LINE__);
}
return($conn);
}
function db_do_query($conn, $statement)
{
$stid = oci_parse($conn, $statement);
if (!$stid) {
db_error($conn, __FILE__, __LINE__);
}
$r = oci_execute($stid, OCI_DEFAULT);
if (!$r) {
db_error($stid, __FILE__, __LINE__);
}
$r = oci_fetch_all($stid, $results, null, null,
OCI_FETCHSTATEMENT_BY_ROW);
return($results);
}
// $r is the resource containing the error.
// Pass no argument or false for connection errors
function db_error($r = false, $file, $line)
{
$err = $r ? oci_error($r) : oci_error();
if (isset($err['message'])) {
$m = htmlentities($err['message']);
}
else {
$m = 'Unknown DB error';
}
echo '<p><b>Error</b>: at line '.$line.' of '.$file.'</p>';
echo '<pre>'.$m.'</pre>';
exit;
}
?>
The db_do_query() has been written to use the oci_fetch_all() OCI8
function, instead of oci_fetch_array(). The oci_fetch_all() function
accepts five parameters.
Q $stid, the statement identifier for the statement executed
Q $results, the output array variable containing the data returned for the
query
Q The null in the third parameter for the number of initial rows to skip is
ignored.