Search code examples
phpplsqlrecord

Handle a PL/SQL function returning a record in PHP


I've been looking on the Internet and I didn't find an answer so here I am.

I've a PL/SQL function returning a record :

create or replace package pck_test is
    TYPE coord_geo is record (coord_x float, coord_y float);
    function ArrondiGeo(coord_x float, coord_y float) return coord_geo;
end pck_test;
/

create or replace package body pck_test is
    FUNCTION ArrondiGeo(coord_x FLOAT,coord_y FLOAT) RETURN coord_geo
    IS
        temp_x FLOAT(24);
        temp_y FLOAT(24);
        rec_coord coord_geo;
    BEGIN
        temp_x := ROUND(coord_x,4)/5;
        temp_y := ROUND(coord_y,4)/5;

        temp_x := ROUND((temp_x*5),3);
        temp_y := ROUND((temp_y*5),3);

        rec_coord.coord_x := temp_x;
        rec_coord.coord_y := temp_y;

        RETURN rec_coord;
    END;
END pck_test;
/

And I want to use it in a PHP function but I don't really know how ...

I tried this but it doesn't work :

public function get_tronc($x,$y)
    {
        $q = oci_parse($this->_db, 'begin :r := pck_test.ArrondiGeo(:x,:y); end;');
        oci_bind_by_name($q, ':x', $x);
        oci_bind_by_name($q, ':y', $y);
        oci_bind_by_name($q, ':r', $r);
        oci_execute($q);

        return $r;
    }

The error is :

Warning: oci_execute(): ORA-06550: line 1, column 13: PLS-00382: expression is of wrong type ORA-06550: line 1, column 7: PL/SQL: Statement ignored in /users/info/il3/jboeglin/Bureau/BDD/site/models/userManager.php on line 77 

So that's a self explaining error but I still can't figure how I can use it.

Thank you.


Solution

  • For a named datatype, you probably need to bind your parameter specifying the SQLT_NTY type:

        $r = oci_new_collection($this->db, 'COORD_GEO');
        oci_bind_by_name($q, ':r', $r, -1, SQLT_NTY);
        ...
        oci_execute($q);
    
        // do whatever you need with your data
        $data = $elem = $collection->getElem(1);
    
        // then discard it
        $r->free();
    

    See the manual of oci_bind_by_name for details.


    For plain PL/SQL records, you're probably out of luck: according to Oracle's documentation you can't use OCI to fetch a record. As of Oracle 11g:

    The following two types are internal to PL/SQL and cannot be returned as values by OCI:

    • Boolean, SQLT_BOL
    • Record, SQLT_REC

    If this is correct (I'm not a regular PHP user), you will probably have to wrap your function at PL/SQ level:

    • either, in a procedure with the required number of OUT parameters;
    • or maybe, depending your needs, in a table function.

    For sake of completeness, please note that Oracle 12c + OCI8 2.0.7 remove the restriction that had previously not allowed to return a boolean value.