简体   繁体   English

PHP Oracle事务插入错误

[英]PHP Oracle transaction insert error

I've wasted enough time on this and I need your help. 我在此上浪费了足够的时间,需要您的帮助。 It's probably a simple oversight but I use this function to connect to an oracle database a perform a transaction: 这可能是一个简单的疏忽,但是我使用此函数连接到oracle数据库并执行事务:

function transaction($query, $loop, $read_only, $return_vals){
     $DEBUG = 'yes';

    $db_name_idde = "server";
    $db_user_idde = "schema";
    $db_pass_idde = "password";

    $connection = oci_new_connect($db_user_idde, $db_pass_idde, $db_name_idde);

    $return_vars = array();

    //foreach always needs an array
    if( gettype($query) == 'string'){
        $query = array(0 => $query);
    }

    foreach($query as $statement){

        $stid = oci_parse($connection, $statement);

        if (!$stid && $DEBUG == "yes") {
            die(oci_error($connection));
        } else if (!$stid) {
            die('Could Not Execute Query.');
        }


        if ( !oci_execute($stid, OCI_DEFAULT) && $DEBUG == "yes") {

        // If we have a problem, rollback then die
            oci_rollback($connection);
            oci_close($connection);

            die($statement);
            //die(oci_error($stid));
        } else if ( !oci_execute($stid, OCI_DEFAULT) ) {
            oci_rollback($connection);
            oci_close($connection);

            die('Could Not Execute Query.');
        }
    }

    oci_commit($connection);
    oci_close($connection);

    // See if we need to send the results back
    if ($return_vals) {
        if ($loop) {
            $i = 0;
            while ($row = oci_fetch_array($stid, (OCI_RETURN_NULLS+OCI_ASSOC))) {
                $return_vars[$i] = $row;

                $i ++;
            }
        } else {
            $return_vars = oci_fetch_array($stid, (OCI_RETURN_NULLS+OCI_ASSOC));
        }
    } else {
        //$return_vars = mysql_insert_id($connection);
    }

    global $db_num_results;
    global $zero;
    $db_num_results = $i;
    $zero = 0;

    return $return_vars;
}

Now this function can process Updates but always fails with inserts, INSERT INTO or INSERT ALL, claiming a duplicate key for the randomly generated OBJECTID. 现在,此函数可以处理更新,但是始终无法执行插入,INSERT INTO或INSERT ALL的操作,并声明随机生成的OBJECTID的重复密钥。 I started using mt_rand instead and it generates much longer IDs than anything in the DB so it shouldn't make this claim at all: 我开始改用mt_rand,它生成的ID比数据库中任何东西都要长得多,因此它根本不应该声明:

INSERT ALL

INTO table1 (OBJECTID, OUTFALL_ID, INVESTIGATION_ID, LOGGED_DATE,LOGGED_BY,ASSET_ID,SOURCE,COMMENTS,STATUS, CREATED_BY, CREATED_DATE) 
VALUES ( 1314897731, '10C10010', TO_CHAR(SYSDATE, 'YYYY') || '-' || sequence.NEXTVAL,  TO_DATE('05/15/2014','mm/dd/yyyy'),'person','','Sample','','Open', 'a@b.com', SYSDATE)

INTO table2 (OBJECTID, OUTFALL_ID, INVESTIGATION_ID, TASK_DATE,TASK_TIME,ENTERED_BY,TASK_TYPE,DOCUMENT_LINK,SUMMARY, CREATED_BY, CREATED_DATE) 
VALUES ( 1314897731, '10C10010', TO_CHAR(SYSDATE, 'YYYY') || '-' || sequence.CURRVAL,  TO_DATE('05/24/2014','mm/dd/yyyy'),'','person','Initial Inspection','','', 'a@b.com', SYSDATE) SELECT * FROM DUAL 

However when I run this statement in TOAD there aren't any complaints and there shouldn't be considering the new IDs I'm generating are at least 10x the size of the ones currently in there. 但是,当我在TOAD中运行此语句时,没有任何抱怨,也不应考虑我正在生成的新ID至少是当前ID的10倍。 Maybe I left some PL/SQL in there, or its accidentally repeating itself. 也许我在其中留下了一些PL / SQL,或者意外地重复了它。 I don't know. 我不知道。

The culprit is this: 罪魁祸首是:

!oci_execute($stid, OCI_DEFAULT)

For some reason this forced the unique constraint error which is unusual considering it returns true/false and could be used in the if statement's conditional. 由于某种原因,这迫使唯一性约束错误,考虑到它返回true / false,这是不常见的,可以在if语句的条件中使用。 Changing it to 更改为

$r = oci_execute($stid, OCI_DEFAULT);
if(!$r && $DEBUG == 'YES'){

removed the error. 消除了错误。 This does not explain why updates worked correctly but I have not had any more trouble since I enacted this change. 这不能解释为什么更新正确地起作用,但是自从我实施了此更改以来,我再也没有遇到任何麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM