Database operations classes that encapsulate Oracle functions

Source: Internet
Author: User
Tags ereg connect query stmt

<?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:
*
*-for your 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 a SQL statement
* Returns:false-query failed
* intege R-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 the data from a fetched row.
* Returns:false-error
* (array)-returned row, 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 and 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 (the 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 (the 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
}

?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.