Call Oracle stored procedure from PHP

后端 未结 2 1603
灰色年华
灰色年华 2020-12-18 04:15

I am trying to execute and get data from my procedure:

Here is the way my procedure is defined:

create or rep         


        
相关标签:
2条回答
  • 2020-12-18 04:58

    To work with a cursor in PHP three additional steps are required, as compared to accessing rows directly from a SELECT statement.

    • The first step is preparing a cursor resource in PHP using the oci_new_cursor() function, which you then use to bind to the appropriate parameter.
    • The second step is to add a parameter on oci_bind_by_name() function
    • The third step, after you have executed the usual SQL statement, is calling oci_execute() on the cursor resource.

    The code:

    //Connection does not change
    $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = JXYX.com)(PORT = 1521)))(CONNECT_DATA=(SID=DHSJKS)))";
    $conn = ocilogon("XXXXXX","XXXXXXXX",$db);          
    
    //Request does not change
    $sql = 'BEGIN SP_GET_MY_DATA(:POP, :SEG, :DUR, :VIEW, :PAGE, :OUTPUT_CUR); END;';            
    
    //Statement does not change
    $stmt = oci_parse($conn,$sql);                     
    oci_bind_by_name($stmt,':POP',$pop);           
    oci_bind_by_name($stmt,':SEG',$seg);           
    oci_bind_by_name($stmt,':DUR',$dur);           
    oci_bind_by_name($stmt,':VIEW',$view);           
    oci_bind_by_name($stmt,':PAGE',$page);    
    
    //But BEFORE statement, Create your cursor
    $cursor = oci_new_cursor($conn)
    
    // On your code add the latest parameter to bind the cursor resource to the Oracle argument
    oci_bind_by_name($stmt,":OUTPUT_CUR", $cursor,-1,OCI_B_CURSOR);
    
    // Execute the statement as in your first try
    oci_execute($stmt);
    
    // and now, execute the cursor
    oci_execute($cursor);
    
    // Use OCIFetchinto in the same way as you would with SELECT
    while ($data = oci_fetch_assoc($cursor, OCI_RETURN_LOBS )) {
        print_r($data);
    }
    

    I'm not very fluent with Oracle (and english) so you should read this tutorial. There is an interesting example, look at the Stored Procedures and Reference Cursors chapter!

    Hope it helps!

    0 讨论(0)
  • 2020-12-18 05:03

    This example is based in Oracle Documentation

    // Define MYDB connection string as described in tnsnames.ora
    define("MYDB","(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = JXYX.com)(PORT = 1521)))(CONNECT_DATA=(SID=DHSJKS)))");
    // Connect to database
    $conn = oci_connect("XXXXXX","XXXXXXXX",MYDB);  
    // Through error if not connected 
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }  
    // Bind the your input and output parameters to PHP variables                
    $stmt = oci_parse($conn,'BEGIN SP_GET_MY_DATA(:POP, :SEG, :DUR, :VIEW, :PAGE, :OUTPUT_CUR); END;');                     
    oci_bind_by_name($stmt,':POP',$pop);           
    oci_bind_by_name($stmt,':SEG',$seg);           
    oci_bind_by_name($stmt,':DUR',$dur);           
    oci_bind_by_name($stmt,':VIEW',$view);           
    oci_bind_by_name($stmt,':PAGE',$page);  
    // Declare your cursor         
    $OUTPUT_CUR = oci_new_cursor($conn);
    oci_bind_by_name($stmt,":OUTPUT_CUR", $OUTPUT_CUR, -1, OCI_B_CURSOR);    
    // Execute statement               
    oci_execute($stmt); 
    // Execute the cursor
    oci_execute($OUTPUT_CUR);
    // Fetch results
    while ($data = oci_fetch_assoc($OUTPUT_CUR)) {
        print_r($data);
    }
    
    0 讨论(0)
提交回复
热议问题