Oracle
 SQL >> Base de données >  >> RDS >> Oracle

Appeler une procédure stockée Oracle avec un curseur de retour depuis PHP

Renvoyer un curseur de PL/SQL vers PHP demande trois étapes supplémentaires par rapport à la lecture directe de lignes depuis un SELECT :

  • Préparer une ressource curseur avec oci_new_cursor(), que vous lierez ensuite au paramètre PL/SQL.
  • Lier cette ressource via oci_bind_by_name() avec le type OCI_B_CURSOR.
  • Après l'exécution de l'instruction, appeler oci_execute() une seconde fois — cette fois sur la ressource curseur elle-même.

Le 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); } 

Je ne suis pas très à l'aise avec Oracle (ni avec l'anglais), donc lisez aussi ce tutoriel : le chapitre Stored Procedures and Reference Cursors propose un exemple complet bien mené.

J'espère que cela vous aide !