Home > php教程 > php手册 > ThinkPHP执行Oracle存储过程

ThinkPHP执行Oracle存储过程

WBOY
Release: 2016-06-07 11:43:44
Original
1607 people have browsed it

ThinkPHP+Oracle执行存储过程。
一、首先修改ThinkPHP3.0的Oracle驱动(ThinkPHP\Lib\Driver\Db\DbOracle.class.php),在DbOracle类中增加以下方法:    /**<br>      +----------------------------------------------------------<br>      * 执行存储过程<br>      +----------------------------------------------------------<br>      * @author   hongping <hongping626><br>      +----------------------------------------------------------<br>      * @param  string $pName  存储过程名称  <br>      * @param  array  $pValue 传入参数值<br>      +----------------------------------------------------------<br>      * @return array  返回执行结果<br>      +----------------------------------------------------------<br>      */<br>      public function execProcedure($pName,$pValue) {<br>         $pValue = array_change_key_case($pValue, CASE_LOWER);//强制下标为小写<br>         $this->initConnect(true);<br>         if ( !$this->_linkID ) return false;<br>         //更改事务模式<br>         $this->mode = OCI_COMMIT_ON_SUCCESS;<br>         //释放前次的查询结果<br>         if ( $this->queryID ) $this->free();<br>         N('db_write',1);<br>         // 记录开始执行时间<br>         G('queryStartTime');        <br>         $argSql = "SELECT ARGUMENT_NAME,IN_OUT FROM USER_ARGUMENTS WHERE OBJECT_NAME = '".strtoupper($pName)."' ORDER BY SEQUENCE";        <br>         $argRow = $this->query($argSql);<br>         $ParStr = '';<br>         $x = 0;   <br>         foreach($argRow as $key=>$value){<br>                 if($x==0){<br>                     $ParStr .= ":".$value['argument_name'];                    <br>                 }else{<br>                     $ParStr .= ",:".$value['argument_name'];                    <br>                 }<br>                 $x++;<br>         }<br>         $this->queryID = oci_parse($this->_linkID, 'BEGIN '.$pName.'('.$ParStr.');END;');<br>         foreach($argRow as $key=>$value){<br>           $parmName = strtolower($value['argument_name']);<br>           $parmType = strtolower($value['in_out']);<br>           if($parmType==='out'){<br>             oci_bind_by_name($this->queryID, ":".$parmName, $OutData[$parmName],2048);<br>           }else{<br>             $OutData[$parmName] = $pValue[$parmName];<br>             oci_bind_by_name($this->queryID, ":".$parmName, $OutData[$parmName],2048);<br>           }<br>         }         <br>         oci_execute($this->queryID);             <br>         $this->debug();<br>         if (!$this->queryID) {<br>             $this->error();<br>             return $this->queryID;<br>         }else {<br>             return $OutData;<br>         }<br>     }</hongping626>二、然后在ThinkPHP\Lib\Core\Model.class.php加入以下(1185行左右):/** <br>      +---------------------------------------------------------- <br>      * 执行oracle存儲過程 <br>      +---------------------------------------------------------- <br>      * @access public <br>      +---------------------------------------------------------- <br>      * @param string  $sql    SQL指令 <br>      * @param array   $parr   传入传出参数值 <br>      * @param boolean $parse  是否需要解析SQL <br>      +---------------------------------------------------------- <br>      * @return false | integer <br>      +---------------------------------------------------------- <br>      */  <br>     public function execProcedure($sql,$parr,$parse=false) {  <br>         $sql  =   $this->parseSql($sql,$parse);  <br>         return $this->db->execProcedure($sql,$parr);  <br>     } 使用示例:public function testPub(){            <br>          $data['name'] =  $_POST['name']; //存储过程传入参数<br>          $data['value'] =   $_POST['value']; //存储过程传入参数<br>            <br>          $pdName = "PUB_TEST_KT";/*存储过程名*/  <br>          $result =M() ->execProcedure($pubName,$pubParm);//执行<br>          $this->success($result['res']);//返回结果

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template