📄 00000031.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: thinkin (强强), 信区: Linux <BR>标 题: A Small Oracle Class <BR>发信站: BBS 水木清华站 (Thu Feb 17 11:09:24 2000) <BR> <BR><?PHP <BR>/* <BR> Defination of ORACLE db CLASS and ORACLE query CLASS <BR> Author: Likai <BR>*/ <BR>if ( !defined( "_ORACLE_CLASS" ) ) <BR>{ <BR> define( "_ORACLE_CLASS", 1 ); <BR>/* <BR> ORACLE database class <BR> function open :open oraclass database connection <BR> function close: close the database connection and free the smtm <BR> function addqury: add the smtm ready for free <BR>*/ <BR>class ora_db <BR>{ <BR> var $connect_id; <BR> //function to open ORACLE database <BR> function open($database, $user, $password) <BR> { <BR> $this->connect_id = OCILogon($user, $password, $database); <BR> if( $this->connect_id ) <BR> return $this->connect_id; <BR> else <BR> return FALSE; <BR> } <BR> // Closes the database connection and frees any query results left. <BR> function close() <BR> { <BR> if($this->stmt_id && is_array($this->stmt_id)) <BR> { <BR> while (list($key,$val)=each($this->stmt_id)) <BR> { <BR> @OCIFreeStatement($val) ; <BR> } <BR> } <BR> $result = OCILogoff($this->connect_id); <BR> return $result; <BR> } <BR> // Function used by the constructor of query. <BR> function addstmt($stmt_id) <BR> { <BR> $this->stmt_id[]=$stmt_id; <BR> } <BR> } <BR>/* End of Defination of ORACLE db Class */ <BR>/* <BR> Defination of ORACLE query Class <BR>*/ <BR> class ora_query <BR> { <BR> var $row; <BR> var $smtm; <BR> var $bind_data; <BR> //new a query and directly execute the normal query <BR> function ora_query(&$db, $query= "") <BR> { <BR> if($query!= ""&&$db->connect_id) <BR> { <BR> $this->stmt = OCIParse($db->connect_id, $query); <BR> if (!$this->stmt) <BR> { <BR> return false; <BR> } <BR> if (OCIExecute($this->stmt)) <BR> { <BR> $db->addstmt($this->stmt); <BR> return $this->stmt; <BR> } <BR> OCIFreeStatement($this->stmt); <BR> return false; <BR> } <BR> else <BR> return FALSE; <BR> } <BR> /* <BR> before execute the query ,BIND the query with the php extern variables <BR> for example $query = "insert into ? values(?,'?',?,0) <BR> then the function analye the string <BR> */ <BR> function prepare(&$db,$query= "") <BR> { <BR> $pieces = explode( "?", $query); <BR> $new_query = ""; <BR> $last_piece = sizeof($pieces) - 1; <BR> while (list($i, $piece) = each($pieces)) <BR> { <BR> $new_query .= $piece; <BR> if ($i < $last_piece) <BR> { <BR> $new_query .= ":var$i"; <BR> } <BR> } <BR> print "<br>new_query=$new_query\n<br>"; <BR> //for debug <BR> $this->stmt = OCIParse($db->connect_id, $new_query); <BR> if (!$this->stmt) <BR> { <BR> return false; <BR> } <BR> for ($i = 0; $i < $last_piece; $i++) <BR> { <BR> $bindvar = ":var$i"; <BR> if (!OCIBindByName($this->stmt, $bindvar, &$this->bind_data[$i],3 <BR>2)) <BR> { <BR> OCIFreeStatement($this->stmt); <BR> return FALSE; <BR> } <BR> } <BR> return $this->stmt; <BR> } <BR> //after prepare query then execute the query <BR> function execute(&$db,$data) <BR> { <BR> while (list($i, $value) = each($data)) <BR> { <BR> $this->bind_data[$i] = $data[$i]; <BR> echo $this->bind[$i]; <BR> } <BR> if (OCIExecute($this->stmt)) <BR> { <BR> $db->addstmt($this->stmt); <BR> return $this->stmt; <BR> } <BR> return FALSE; <BR> } <BR> // fetch the next row of stmt <BR> function getrow() <BR> { <BR> if(!OCIFetchInto($this->stmt, &$this->row,OCI_ASSOC)) <BR> { <BR> return FALSE; <BR> } <BR> var_dump($this->row); <BR> return $this->row; <BR> } <BR> // get the number of rows of $stmt <BR> function numrows() <BR> { <BR> $number=OCIRowCount($this->stmt) ; <BR> return $number; <BR> } <BR> //get oracle runtime error <BR> function error() <BR> { <BR> $error=OCIError($this->stmt); <BR> if( $error ) <BR> return $errot; <BR> else <BR> return OCIError($db->connect_id); <BR> } <BR> } <BR>/* End of Defination of ORACLE query Class */ <BR>} <BR>/*End ifdefined */ <BR>//example: <BR>$DB = new ora_class; <BR>$DB->open( "", "scott", "tiger"); <BR>$q = "Select * from scott.emp"; <BR>$test_query = new ora_query($DB,$q); <BR>while($test_query->getrow()) <BR>{ <BR> echo "<br>".$test_query->row[ "JOB"]; <BR>} <BR>$q = "Insert into testtable values(?,?,?,?); <BR>$data[] <BR>Parse error: parse error, expecting `STRING' or `NUM_STRING' or `'$'' in /ht <BR>ml/px.sklar.com/code-pretty.html on line 5 <BR> <BR>-- <BR> <BR>人生到处知何似? <BR> 应似飞鸿踏雪泥。 <BR> 泥上偶然留指爪, <BR> 鸿飞那复计东西! <BR> <BR> <BR>※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.37.191] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -