Home php教程 php手册 操作Oracle的php类

操作Oracle的php类

Jun 13, 2016 pm 12:42 PM
oracle php Revise operate not yet of kind warn


//【警告】:未经许可请勿随便修改
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//   
// 【文件名】:                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            {
                $A_define_up[$i] = trim(strtoupper($A_define[$i]));    //大写并去除空格
            }
            for($i=0;$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 "执行出错:SQL Error:$C_sql
";
            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                    {
                        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                                {
                                    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                }                 
            }

        }     //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            {
                global $$A_bind[$i];
                $$A_bind[$i] = StripSlashes($$A_bind[$i]);            //去掉反斜线字元
                $$A_bind[$i] = str_replace("","            }
            for($i=0;$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("","            OCIBindByName($this -> I_stmtID, ":$arrBind", &$$A_bind, -1);                 
        }

        //执行并检测是否成功
        if(!OCIExecute($this -> I_stmtID,OCI_DEFAULT))
        {
            echo "执行出错:SQL Error:$C_sql
";
            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  (OCIRollback($this->I_linkID));
    }     
//------------------------------------------------------------------------------------------

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

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

See all articles