Quick Search:
 
 Oracle PL/SQL: PHP to Oracle 8 DB abstraction Jump to:  
Category: >> Oracle PL/SQL >> PHP to Oracle 8 DB abstraction  

<< lastnext >>

Snippet Name: PHP to Oracle 8 DB abstraction

Description: A database abstraction layer for the PHP Oracle 8 module.

Comment: (none)

Language: PHP, ORACLE
Highlight Mode: PHP
Last Modified: February 27th, 2009

<?PHP // -*- C++ -*-
/*
* $Id: db-oci8.phl,v 1.2 1998/10/01 19:16:06 ssb Exp $
*/
 
$db_error_code = 0;
$db_error_msg = FALSE;
$db_error_source = FALSE;
 
/*
* Database specific notes:
*
* - You must configure Oracle listener to use this abstraction layer.
*
*/
 
 
/**
* @function db_connect
* @purpose Connect to a database
* @desc
* Connects to a database and returns and identifier for the connection.
* @arg database
* Data source name or database host to connect to.
* @arg user
* Name of user to connect as.
* @arg password
* The user's password.
*/
FUNCTION db_connect($database, $user, $password)
{
$ret = @OCILogon($user, $password, $database);
db_check_errors($php_errormsg);
RETURN $ret;
}
 
/*
* Function: db_query
* Arguments: $conn (int) - connection identifier
* $query (string) - SQL statement to execute
* Description: executes an SQL statement
* Returns: false - query failed
* integer - query succeeded, value is result handle
*/
FUNCTION db_query($conn, $query)
{
$stmt = @OCIParse($conn, $query);
db_check_errors($php_errormsg);
IF (!$stmt) {
RETURN FALSE;
}
IF (@OCIExecute($stmt)) {
RETURN $stmt;
}
db_check_errors($php_errormsg);
@OCIFreeStatement($stmt);
RETURN FALSE;
}
 
/*
* Function: db_fetch_row
* Arguments: $stmt (int) - result identifier
* Description: Returns an array containing data from a fetched row.
* Returns: false - error
* (array) - returned row, first column at index 0
*/
FUNCTION db_fetch_row($stmt)
{
$cols = @OCIFetchInto($stmt, &$row);
IF (!$cols) {
db_check_errors($php_errormsg);
RETURN FALSE;
}
RETURN $row;
}
 
/*
* Function: db_free_result
* Arguments: $stmt (int) - result identifier
* Description: Frees all memory associated with a result identifier.
* Returns: false - failure
* true - success
*/
FUNCTION db_free_result($stmt)
{
GLOBAL $db_oci8_pieces;
 
IF (ISSET($db_oci8_pieces[$stmt])) {
UNSET($db_oci8_pieces[$stmt]);
}
$ret = @OCIFreeStatement($stmt);
db_check_errors($php_errormsg);
RETURN $ret;
}
 
/*
* Function: db_disconnect
* Arguments: $connection (int) - connection identifier
* Description: closes a database connection
* Returns: false - failure
* true - success
*/
FUNCTION db_disconnect($connection)
{
$ret = @OCILogoff($connection);
db_check_errors($php_errormsg);
RETURN $ret;
}
 
/*
* Function: db_autocommit
* Arguments: $connection (int) - connection identifier
* Description: turn autocommit on or off
* Returns: false - failure
* true - success
*/
FUNCTION db_autocommit($connection, $enabled){
IF (!$enabled) {
db_post_error(0, "Transactions not yet implemented");
RETURN FALSE;
}
RETURN TRUE;
}
 
 
FUNCTION db_commit($connection){
RETURN TRUE;
}
 
 
FUNCTION db_rollback($connection){
db_post_error(0, "Transactions not yet implemented");
RETURN FALSE;
}
 
 
FUNCTION db_quote_string($string){
$ret = EREG_REPLACE( "'", "''", $string);
RETURN $ret;
}
 
 
FUNCTION db_prepare($connection, $query)
{
GLOBAL $db_oci8_pieces;
 
$pieces = EXPLODE( "?", $query);
$new_query = "";
$last_piece = SIZEOF($pieces) - 1;
PRINT "<br>last_piece=$last_piece\n";
WHILE (LIST($i, $piece) = EACH($pieces)) {
$new_query .= $piece;
IF ($i < $last_piece) {
$new_query .= ":var$i";
}
}
PRINT "<br>new_query=$new_query\n";
$stmt = @OCIParse($connection, $new_query);
IF (!$stmt) {
db_check_errors($php_errormsg);
RETURN FALSE;
}
FOR ($i = 0; $i < $last_piece; $i++) {
$bindvar = ":var$i";
PRINT "<br>trying to bind $bindvar\n";
IF (!@OCIBindByName($stmt, $bindvar, &$db_oci8_pieces[$stmt][$i])) {
db_check_errors($php_errormsg);
@OCIFreeStatement($stmt);
RETURN FALSE;
}
}
RETURN $stmt;
}
 
 
FUNCTION db_execute($stmt, $data){
GLOBAL $db_oci8_pieces;
 
WHILE (LIST($i, $value) = EACH($data)) {
$db_oci8_pieces[$stmt][$i] = $data[$i];
}
$ret = @OCIExecute($stmt);
IF (!$ret) {
db_check_errors($php_errormsg);
RETURN FALSE;
}
RETURN TRUE;
}
 
 
FUNCTION db_error_code(){
GLOBAL $db_error_code;
RETURN $db_error_code;
}
 
 
FUNCTION db_error_msg(){
GLOBAL $db_error_msg;
RETURN $db_error_msg;
}
 
 
FUNCTION db_error_source(){
GLOBAL $db_error_source;
RETURN $db_error_source;
}
 
 
FUNCTION db_check_errors($errormsg){
GLOBAL $db_error_code, $db_error_msg, $db_error_source;
IF (EREG( '^([^:]*): (...-.....): (.*)', $errormsg, &$data)) {
LIST($foo, $function, $db_error_code, $db_error_msg) = $data;
$db_error_msg = "$function: $db_error_msg";
$db_error_source = "[Oracle][PHP][OCI8]";
} ELSEIF (EREG( '^([^:]*): (.*)', $errormsg, &$data)) {
LIST($foo, $function, $db_error_msg) = $data;
$db_error_msg = "$function: $db_error_msg";
$db_error_code = 0;
$db_error_source = "[PHP][OCI8][db-oci8]";
} ELSE {
$db_error_msg = $errormsg;
$db_error_code = 0;
$db_error_source = "[PHP][OCI8][db-oci8]";
}
}
 
 
FUNCTION db_post_error($code, $message){
GLOBAL $db_error_code, $db_error_msg, $db_error_source;
$db_error_code = $code;
$db_error_msg = $message;
$db_error_source = "[PHP][OCI8][db-oci8]";
}
 
 
FUNCTION db_api_version(){
RETURN 10; // 1.0
}
 
?>
 
 


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org