博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个PHP 连接 Oracle 10g的类
阅读量:6301 次
发布时间:2019-06-22

本文共 4163 字,大约阅读时间需要 13 分钟。

  hot3.png

PHP CLASS for ORACLE

This is the database connectivity script of PHP and ORACLE all the functional work are in the single file such as query,fetch, generate ID etc so everyone who really wants or working in php + oracle may get more help from it

class.db.php
<?php
class db_connect{
      var $sid, $connection;
      var $query;
      var $host;
      var $total_record,$rec_position;
      var $total_fields, $field_name;
      /*This function connect to the database . This function is called
      whenever object is created. */

        function db_connect(){

                $this->host="".ORCL_HOST."";
                $this->sid="(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=".ORCL_HOST.")(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=".ORCL_SERVICE_NAME.")))";
                $this->connection = ocilogon(ORCL_USER_NAME,ORCL_PASSWORD,$this->sid) or die ($this->get_error_msg($this->connection,"Problem while connecting to ".$this->sid." server..."));//username,password,sid
           }

       /*This function query at database */
       function db_query($query_str="")
       {
           $this->sql=$query_str;
           $this->rec_position=0;
          // if($query_str==""){
           //   $query_str=$this->query_stmt;
           //}
           $this->query = @ociparse($this->connection, $query_str);
           ociexecute($this->query)or die($this->get_error_msg($this->query ,"Query Error : ".$query_str));
       }

       /*This function query at database which returns TRUE if SUCCESSFUL and FALSE if UNSUCCESSFUL */

       function db_query_return($query_str="",$db=""){
           if($query_str==""){
              $query_str=$this->query_stmt;
           }
           $this->query = ociparse($this->connection, $query_str);
            if($db=="Default") {
                   return ociexecute($this->query,OCI_DEFAULT);
           } else {
                   return ociexecute($this->query);
           }
       }

    function selectBox($sql='',$selected='',$name='select',$multiple='',$size='',$parameter=NULL)

    {
        $this->db_query($sql);
        $output="<select name='$name' $multiple $size $parameter>";
        $output.="<option value=''>--Select--</option>";
        while($result=$this->db_fetch_array())
        {
            if($result[0]==trim($selected)) {
                $a="selected";   
            } else {
                $a="";
            }
            $output.="<option value=".$result[0]." $a>".$result[1]."</option>";
        }
        $output.="</select>";
        echo $output;
        unset($output);
    }
   
    function db_fetch_val()
    {
        $result=$this->db_fetch_array(0);
        return $result[0];
    }

    function gen_id($table_name='',$field='',$increment='') {

         if(empty($increment)) {
             $increment=1;
         } else {
             $increment=$increment;
         }
        $sql="select nvl(max($field),0) + $increment from $table_name";
        $this->db_query($sql);
        return $this->db_fetch_val();
    }
     
       function db_fetch_array($fetch_type=0,$db="DEFAULT"){
        );
       
           if(!is_array($result))
              return false;
              $this->total_field=OCINumCols($this->query);
                     if($db=="DEFAULT"){
                           foreach($result as $key=>$val){
                                   $result[$key]=trim($val);
                                   $result[$key]=trim(htmlspecialchars($val));
                           }
                     }
           return $result;
       }

 

       function get_field_name($i){

           return OCIColumnName($this->query, $i+1);
       }

       function get_num_fields() {
           return @ocinumcols($this->query);
       }

       function get_field_type($i, $sql=""){

               return ocicolumntype($this->query, $i+1);
       }
       
       function get_field_size($i, $sql=""){
               return ocicolumnsize($this->query, $i+1);
       }
       

       function total_record(){

          return oci_num_rows($this->query);
       }

       function free(){
          ocifreestatement($this->query);
          ocilogoff($this->connection);
          unset($this);
       }

       function db_fetch_tr ($css="",$colname='y',$add='y',$update='y',$delete='y'){
          if($css!=""){
              $css_val="class=".$css;
          }
          if(!empty($colname)) {
              echo "<tr $css_val>";
              for ($i=0; $i< $this->get_num_fields(); $i++) {
                   echo "<td>".$this->get_field_name($i)."<td>";
              }
              //echo "<td>Update<td>Delete";
              echo "</tr>";
          }
          while($result=$this->db_fetch_array(1)){
                    echo "<tr $css_val>";
                           for ($j=0; $j<$this->get_num_fields(); $j++) {
                               $cname=$this->get_field_name($j);
                                   echo "<td>".$result[$cname]." <td>";
                           }
                    echo "</tr>";
          }

       }

       function get_error_msg($error_no,$msg=""){
          $log_msg=NULL;
          $error_msg="<b>Custom Error :</b> <pre><font color=red>\n\t".ereg_replace(",",",\n\t",$msg)."</font></pre>";
          $error_msg.="<b><i>System generated Error :</i></b>";
          $error_msg.="<font color=red><pre>";
                foreach(ocierror($error_no) as $key=>$val){
                        $log_msg.="$key :  ".$val."\n";
                        $error_msg.="$key : $val \n";
                }

                $error_msg.="</pre></font>";

                return $error_msg;
       }

       function get_error_msg_array($error_no){

          return ocierror($error_no);
       }
}
?>

转载于:https://my.oschina.net/alkz/blog/152223

你可能感兴趣的文章
apache中文url日志分析--php十六进制字符串转换
查看>>
Ansible--playbook介绍
查看>>
NETBIOS
查看>>
浅谈代理
查看>>
php创建桌面快捷方式实现方法
查看>>
0927集合作业
查看>>
基于jquery实现的超酷动画源码
查看>>
fl包下的TransitionManager的使用
查看>>
Factorialize a Number
查看>>
[USB-Blaster] Error (209040): Can't access JTAG chain
查看>>
TreeSet的用法
查看>>
防HTTP慢速攻击的nginx安全配置
查看>>
深入理解PHP内核(十四)类的成员变量及方法
查看>>
Spring Boot2.0+中,自定义配置类扩展springMVC的功能
查看>>
参与博客编辑器改版,我的礼物 感谢51cto
查看>>
JavaWeb笔记——JSTL标签
查看>>
Eclipse插件大全 挑选最牛的TOP30
查看>>
一些实用性的总结与纠正
查看>>
Kubernetes概念
查看>>
逻辑卷管理器(LVM)
查看>>