欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

操作Oracle的php类

程序员文章站 2022-11-01 10:58:27
<?
//【警告】:未经许可请勿随便修改
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//   
// 【文件名】:                c_ora_db.inc
// 【作  用】:                oracle公用函数类
// 【作  者】:                天灰
//  
// 【最后修改日期】:        2001/05/11[cxx]      
// 【变量定义规则】:‘c_'=字符型,‘i_'=整型,‘n_'=数字型,‘l_'=布尔型,‘a_'=数组型
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//    ※db_logon()                    开启数据库连接     
//    ※db_query()                    通用select             
//    ※db_change()                    数据库改变的通用函数(insert,delete,update)
//    ※db_insert()                    insert,直接调用db_change()
//    ※db_delete()                    delete,直接调用db_change()
//    ※db_update()                    update,直接调用db_change()                 
//    ※db_commit()                    事务递交
//    ※db_rollback()                    事务回退
//    ※db_logoff()                    断开数据库连接
//------------------------------------------------------------------------------------------


class c_ora_db
{

     
//------------------------------------------------------------------------------------------
//        变量定义
//------------------------------------------------------------------------------------------
    var $c_user          = "";              //数据库用户名
    var $c_passwd        = "";            //数据库口令
    var $c_db            = "";                    //数据库名
    var $i_linkid        = 0;                          //连线句柄
    var $i_stmtid        = 0;                          //查询句柄
    var $color             ="";                    //全局颜色
//------------------------------------------------------------------------------------------



//------------------------------------------------------------------------------------------
//        函数名:db_logon()
//        作  用:开启数据库连接
//        参  数:无
//        返回值:连线句柄(整型)
//        备  注:无
//------------------------------------------------------------------------------------------
    function  db_logon()    
    {    
        $this->i_linkid =  @ocilogon($this->c_user,$this->c_passwd,$this->c_db);
        if ($this->i_linkid == 0){alertexit('数据库链接失败,请与dba联系!');}
        return  $this->i_linkid;    
    }
//------------------------------------------------------------------------------------------

       
//------------------------------------------------------------------------------------------
//        函数名:db_query($c_sql,$a_define="",$i_start=-1,$i_end=-1)
//        作  用:select
//        参  数:$c_sql                    sql语句
//                $a_define                需绑定的字段。数组型         
//                $i_start                开始取记录 -1则取出查询的所有记录
//                $i_end                    结束取纪录
//        返回值:二维数组($a_rs)
//        备  注:通过数字0,1,2....可访问对应字段的值; 或通过查询字段名也可访问对应字段的值
//                如通过$a_rs[0][0]或$a_rs[0]['name']或$a_rs[0]['name']都可访问首条记录name字段
//                $i_start,$i_end是配合分页使用的参数。
//------------------------------------------------------------------------------------------
    function  db_query($c_sql,$a_define="",$i_start=-1,$i_end=-1)
    {    
       if (!$c_sql){alertexit("参数不全!");}//检查参数

       //连接检测
       if ($this->i_linkid == 0){alertexit('数据库链接失败,请与dba联系!');}

       //格式检测
       $this -> i_stmtid = ociparse($this -> i_linkid,$c_sql);     
       if (!$this -> i_stmtid){alertexit(' sql格式出错!请与程序员联系');}

       //如果没指定绑定的字段,则从sql语句中去取
       if($a_define=="")
       {
            $a_cur = explode("select",$c_sql);
            $a_cur = explode("from",$a_cur[1]);
            $a_define = explode(",",$a_cur[0]);
       }

           //绑定数据库表字段
        if(gettype($a_define) == "array")            //查询列是数组
        {
            for($i=0;$i<count($a_define);$i++)
            {
                $a_define_up[$i] = trim(strtoupper($a_define[$i]));    //大写并去除空格
            }
            for($i=0;$i<count($a_define_up);$i++)
            {
                ocidefinebyname($this -> i_stmtid,"$a_define_up[$i]",&$$a_define[$i]);    //绑定
            }
        }
        elseif(trim($a_define) <> "")                //查询列只有一个
        {
            $a_define_up = trim(strtoupper($a_define));
            ocidefinebyname($this -> i_stmtid,"$a_define_up",&$$a_define);
        }

        //执行绑定好的sql语句
        if(!ociexecute($this -> i_stmtid))
        {
            echo "<font color=red><b>执行出错:</b></font>sql error:<font color=red>$c_sql</font><br>";
            return false;
        }

        $lower = 0;                    //返回二维数组的第一维下标控制变量
        $cnt = 0;                    //开始取数标识

        //取记录
        while (ocifetchinto($this -> i_stmtid,&$cur,oci_assoc))
        {
            //取查询出来的所有记录
            if ($i_start == -1)
            {
                if (gettype($a_define) == "array")        //查询列是数组
                {
                    for ($i=0;$i<count($a_define);$i++)
                    {
                        if ($cur[$a_define_up[$i]] <> $$a_define[$i])
                        {
                            $$a_define[$i] = $cur[$a_define_up[$i]];     
                        }
                        $a_rs[$lower][$i] = $$a_define[$i];                    //用数字访问
                        $a_rs[$lower][$a_define[$i]] = $$a_define[$i];        //用小些访问
                        $a_rs[$lower][$a_define_up[$i]] = $$a_define[$i];    //用大写访问
                    }         
                }
                elseif (trim($a_define) <> "")            //查询列只有一个
                {

                    if ($cur[$a_define_up] <> $$a_define)
                    {
                        $$a_define = $cur[$a_define_up];     
                    }
                    $a_rs[$lower][0] = $$a_define;                    //用数字访问
                    $a_rs[$lower][$a_define] = $$a_define;        //用小写访问
                    $a_rs[$lower][$a_define_up] = $$a_define;    //用大些访问
                }
                $lower++;            //下标加一
            }

            //取出指定记录(配合分页使用)
            if ($i_start <> -1)
            {
                if ($cnt >= $i_start)
                {
                    $cnt++;
                    if ($i_end - $i_start <> 0)
                    {
                        $i_end--;
                            if (gettype($a_define) == "array")
                            {
                                for($i=0;$i<count($a_define_up);$i++)
                                {
                                    if ($cur[$a_define_up[$i]] <> $$a_define[$i])
                                    {
                                        $$a_define[$i] = $cur[$a_define_up[$i]];     
                                    }
                                    $a_rs[$lower][$i] = $$a_define[$i];                    //用数字访问
                                    $a_rs[$lower][$a_define[$i]] = $$a_define[$i];        //用小些访问
                                    $a_rs[$lower][$a_define_up[$i]] = $$a_define[$i];    //用大写访问
                                }
                            }elseif(trim($a_define) <> "")
                            {
                                if ($cur[$a_define_up] <> $$a_define)
                                {
                                    $$a_define = $cur[$a_define_up];     
                                }
                                $a_rs[$lower][0] = $$a_define;                    //用数字访问
                                $a_rs[$lower][$a_define] = $$a_define;        //用小些访问
                                $a_rs[$lower][$a_define_up] = $$a_define;    //用大写访问                     
                            }
                        $lower++;
                    }else
                    {
                        break;        //如果$i_end-$i_start=0  表示取完记录并跳出while循环
                    }     
                }else
                {
                    $cnt++;        //如果$cnt<$i_start,$cnt++
                }                 
            }

        }     //while的结束

        //释放句柄并返回查询数据(一个二维数组)
        ocifreestatement($this -> i_stmtid);
        return $a_rs;      

    } //function的结束
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函数名:db_change($c_sql,$a_bind)
//        作  用:db change
//        参  数:$c_sql                        sql语句
//                $a_bind                        需绑定的字段。数组型         
//        返回值:布尔值
//        备  注:insert,delete,update通用
//------------------------------------------------------------------------------------------
    function db_change($c_sql,$a_bind="")
    {
        if (!$c_sql){alertexit("参数不全!");}//检查参数

        //连接检测
        if($this -> i_linkid==""){    alertexit("我们的数据库正忙,请稍后再连接!");}     

        //格式检测
        $this -> i_stmtid = ociparse($this -> i_linkid,$c_sql);     
        if (!$this -> i_stmtid){alertexit(' sql格式出错!请与程序员联系');}

        //绑定
        if(gettype($a_bind) == "array")
        {
            for($i=0;$i<count($a_bind);$i++)
            {
                global $$a_bind[$i];
                $$a_bind[$i] = stripslashes($$a_bind[$i]);            //去掉反斜线字元
                $$a_bind[$i] = str_replace("<?","< ?",$$a_bind[$i]);    //过滤掉php标示
            }
            for($i=0;$i<count($a_bind);$i++){
                ocibindbyname($this -> i_stmtid, ":$a_bind[$i]", &$$a_bind[$i], -1);  //绑定
            }
        }
        elseif(trim($a_bind) <> "")                                //不是数组,是字符
        {
            global $$a_bind;
            $$a_bind = stripslashes($$a_bind);
            $$a_bind = str_replace("<?","< ?",$$a_bind);                //过滤掉php标示
            ocibindbyname($this -> i_stmtid, ":$arrbind", &$$a_bind, -1);                 
        }

        //执行并检测是否成功
        if(!ociexecute($this -> i_stmtid,oci_default))
        {
            echo "<font color=red><b>执行出错:</b></font>sql error:<font color=red>$c_sql</font><br>";
            return false;
        }

        /*//传回受影响的行数
        global $i_changenum;
        $i_changenum = ocinumrows($this -> i_stmtid);*/

        //释放句柄,传回值
        ocifreestatement($this -> i_stmtid);
        return true;
    }
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函数名:db_delete($c_sql)
//        作  用:delete
//        参  数:c_sql                    sql语句
//        返回值:布尔值
//        备  注:该函数只是为了使用直观,本质调用db_change()
//------------------------------------------------------------------------------------------
    function db_delete($c_sql)
    {
        return $this -> db_change($c_sql);
    }
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函数名:db_insert($c_sql,a_bind)
//        作  用:insert
//        参  数:c_sql                    sql语句
//                a_bind                    绑定
//        返回值:布尔值
//        备  注:该函数只是为了使用直观,本质调用db_change()
//------------------------------------------------------------------------------------------
    function db_insert($c_sql,$a_bind="")
    {
        return $this -> db_change($c_sql,$a_bind);
    }
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函数名:db_update($c_sql,a_bind)
//        作  用:update
//        参  数:c_sql                    sql语句
//                a_bind                    绑定
//        返回值:布尔值
//        备  注:该函数只是为了使用直观,本质调用db_change()
//------------------------------------------------------------------------------------------
    function db_update($c_sql,$a_bind="")
    {
        return $this -> db_change($c_sql,$a_bind);
    }
//------------------------------------------------------------------------------------------



//------------------------------------------------------------------------------------------
//        函数名:db_commit()
//        作  用:事务递交
//        参  数:无
//        返回值:布尔值
//        备  注:无
//------------------------------------------------------------------------------------------     
    function db_commit()
    {
        return    (ocicommit($this->i_linkid));
    }     
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函数名:db_rollback()
//        作  用:事务回退
//        参  数:无
//        返回值:布尔值
//        备  注:无
//------------------------------------------------------------------------------------------     
    function db_rollback()
    {
        return  (o*llback($this->i_linkid));
    }     
//------------------------------------------------------------------------------------------

     
//------------------------------------------------------------------------------------------
//        函数名:db_logoff()
//        作  用:断开数据库连接
//        参  数:无
//        返回值:布尔值
//        备  注:无
//------------------------------------------------------------------------------------------     
    function db_logoff()
    {
        return (ocilogoff($this->i_linkid));
    }
//------------------------------------------------------------------------------------------

     
//------------------------------------------------------------------------------------------
}
?>