Important alert: (current site time 7/15/2013 10:15:31 AM EDT)
 

VB icon

A Small Oracle Class

Email
Submitted on: 10/29/2000 5:46:52 PM
By: PHP Code Exchange  
Level: Intermediate
User Rating: By 1 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 21177
 
     a small oracle class with OCI easy to manipulate Oracle database by Likai
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: A Small Oracle Class
// Description:a small oracle class with OCI easy to manipulate Oracle database by Likai
// By: PHP Code Exchange
//**************************************

<?PHP
/* 
Defination of ORACLE db CLASS and ORACLE query CLASS
Author: Likai
*/
if ( !defined( "_ORACLE_CLASS" ) )
{
 define("_ORACLE_CLASS", 1 );
/* 
 ORACLE database class 
 function open :open oraclass database connection 
 function close: close the database connection and free the smtm
 function addqury: add the smtm ready for free
*/
class ora_db
{
 var $connect_id; 
 
 //function to open ORACLE database
 function open($database, $user, $password) 
 {
 $this->connect_id = OCILogon($user, $password, $database); 
 if( $this->connect_id )
return $this->connect_id;
 else
return FALSE;
 }
 
 
 // Closes the database connection and frees any query results left.
 function close()
 {
if($this->stmt_id && is_array($this->stmt_id))
{
 while (list($key,$val)=each($this->stmt_id))
{
 @OCIFreeStatement($val) ;
}
}
$result = OCILogoff($this->connect_id); 
return$result; 
 }
 
 // Function used by the constructor of query.
 function addstmt($stmt_id)
 {
 $this->stmt_id[]=$stmt_id;
 }
 }
 
/* End of Defination of ORACLE db Class */
/* 
 Defination of ORACLE query Class 
 
*/
 class ora_query
 {
var $row;
var $smtm;
var $bind_data;
 
 //new a query and directly execute the normal query
function ora_query(&$db, $query="")
{
if($query!=""&&$db->connect_id)
{
$this->stmt = OCIParse($db->connect_id, $query); 
if (!$this->stmt)
{ 
 return false; 
} 
if (OCIExecute($this->stmt)) 
{ 
 $db->addstmt($this->stmt);
 return $this->stmt; 
} 
OCIFreeStatement($this->stmt); 
return false; 
 }
else
 return FALSE;
}
/* 
before execute the query ,BIND the query with the php extern variables 
for example $query = "insert into ? values(?,'?',?,0)
then the function analye the string 
*/
function prepare(&$db,$query="")
{
$pieces = explode( "?", $query); 
$new_query = ""; 
$last_piece = sizeof($pieces) - 1; 
while (list($i, $piece) = each($pieces)) 
{ 
 $new_query .= $piece; 
 if ($i < $last_piece) 
 { 
$new_query .= ":var$i"; 
 } 
} 
print "<br>new_query=$new_query\n<br>";
//for debug
$this->stmt = OCIParse($db->connect_id, $new_query); 
if (!$this->stmt) 
{ 
return false; 
} 
for ($i = 0; $i < $last_piece; $i++) 
{ 
 $bindvar = ":var$i"; 
 if (!OCIBindByName($this->stmt, $bindvar, &$this->bind_data[$i],32))
 { 
	OCIFreeStatement($this->stmt); 
return FALSE;
 } 
} 
return $this->stmt; 
}
//after prepare query then execute the query
function execute(&$db,$data)
{
while (list($i, $value) = each($data)) 
{ 
 $this->bind_data[$i] = $data[$i]; 
	 echo $this->bind[$i];
} 
if (OCIExecute($this->stmt)) 
{ 
 $db->addstmt($this->stmt);
 return $this->stmt;
	}
return FALSE;
}
// fetch the next row of stmt
function getrow()
{ 
if(!OCIFetchInto($this->stmt, &$this->row,OCI_ASSOC)) 
{ 
 return FALSE;
} 
	var_dump($this->row);
return $this->row;
} 
// get the number of rows of $stmt
function numrows()
{
 $number=OCIRowCount($this->stmt) ;
 return $number;
}
//get oracle runtime error 
function error()
{
$error=OCIError($this->stmt);
if( $error )
 return $errot;
else
 return OCIError($db->connect_id);
}
 
 
 }
 
 
/* End of Defination of ORACLE query Class */
}
/*End ifdefined */
//example:
$DB = new ora_class;
$DB->open("","scott","tiger");
$q = "Select * from scott.emp";
$test_query = new ora_query($DB,$q);
while($test_query->getrow())
{
 echo "<br>".$test_query->row["JOB"];
}
$q = "Insert into testtable values(?,?,?,?);
$data[] = "something";
$data[] = "something";
$data[] = "something";
$data[] = "something";
$test1_query->prepare($DB,$q);
$test1_query->execute($DB,$data);
?>


Other 192 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/3/2006 10:59:06 AMSohrab Navaee-Ardeh

I LOVE TO WORK WITH ORACLE, THEREFORE IF YOU HAVE ANY GOOD INFORMATION ON BANK DATABADES.. PLEASE LOOKING FORWARDS..
BEST WISHES
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.