How to call oracle stored procedure in Codeigniter

落爺英雄遲暮 提交于 2019-12-13 03:17:56

问题


I am new with CI application and oracle database. I want to execute a stored procedure from active record. But I can't get hold of any solid documentation.

Does anyone have any experience with calling stored procs with CodeIgniter and/or Active Record and passing in parameters?

function write_message($type = "debug", $message = "", $description = "")
{
    $_fw =& get_instance();

    $data['LOG_TYPE'] = 4;

    switch ($type)
    {
        case "error":
            $data['LOG_TYPE'] = 1;
            break;

        case "success":
            $data['LOG_TYPE'] = 2;
            break;

        case "message":
            $data['LOG_TYPE'] = 3;
            break;

        case "debug":
            $data['LOG_TYPE'] = 4;
            break;

        default:
            $data['LOG_TYPE'] = 1;
            break;
    }

    $data['LOG_URI_STRING'] = uri_string();

    $data['LOG_MESSAGE'] = $message;

    $data['LOG_DESCRIPTION'] = $description;

    $data['LOG_USER_AGENT'] = $_fw->input->user_agent();

    $data['LOG_USER_ID'] = $_fw->session->userdata('USER_ID') != null?$_fw->session->userdata('USER_ID'):0;

    $data['LOG_ADDEN_ON'] = mktime();

    $data['OPERATION_IP'] = $_fw->input->ip_address();

    $sql = $_fw->db->query("CALL dpe_acl.pkg_SYSTEM_LOGS.addSystemLogs('', '".$data['LOG_TYPE']."', '".$data['LOG_URI_STRING']."', '".$data['LOG_MESSAGE']."', '".$data['LOG_DESCRIPTION']."', '".$data['LOG_USER_AGENT']."', '".$data['LOG_USER_ID']."', '".$data['LOG_ADDEN_ON']."', '".$data['OPERATION_IP']."')");

    return;
}

doing this I am getting a warning like this

Severity: Warning

Message: oci_fetch_assoc(): ORA-24374: define not done before fetch or execute and fetch

Filename: oci8/oci8_result.php

Line Number: 83

Please help... Thanks in advance...


回答1:


This error occurs because you have not bound the in/out parameters correctly before calling the stored procedure. Now I ave not a lot of experience with CI so I don't know how it executes the query string, but I do know that you have to bind the input and output parameters to local variables first, you can't just put them in the query string.

Here is a link to an example that may help you figure this out. http://www.dbmotive.com/ora-24374-define-not-done-before-fetch-or-execute-and-fetch/




回答2:


for store procedure use $this->db->stored_procedure('package or schema','you_store_procedure',$parameters). the $parameters is like this:

$parametros=array( 
   array('name'=>':param','value'=>'pass_variable or value','length'=>-1,'type'=>SQLT_CHR),
   array('name'=>':param','value'=>'pass_variable or value','length'=>-1,'type'=>SQLT_CHR), ....
);

For OUT params, preceded the variable with & and set length value , example array('name'=>':param','value'=>&$param,'length'=>30,'type'=>SQLT_CHR)




回答3:


Try this in your model :

if (!$this->db) {
  $m = oci_error();
  trigger_error(htmlentities($m['message']), E_USER_ERROR);
}

$stid = oci_parse($this->db->conn_id, 'BEGIN PROCEDURE_NAME(:PARAMETER_1,:PARAMETER_2,:OUT_MESSAGE); end;');

oci_bind_by_name($stid, ':PARAMAETER_1',  $PARAMAETER_1,200);
oci_bind_by_name($stid, ':PARAMETER_2',  $PARAMETER_2,200);
oci_bind_by_name($stid, ':OUT_MESSAGE',  $OUT_MESSAGE ,100, SQLT_CHR);

if(oci_execute($stid)){
  $results = $OUT_MESSAGE;
}
oci_free_statement($stid);
oci_close($this->db->conn_id);

return  $results;


来源:https://stackoverflow.com/questions/29033762/how-to-call-oracle-stored-procedure-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!