2012-10-25 12 views
5

Per la clausola SQL IN, come faccio a gestire un numero sconosciuto di parametri quando si associa SQL con PHP OCI8?Parametri PHP OCI8 (numero sconosciuto di) per l'istruzione "IN"

Ad esempio, dato il seguente interrogazione

e l'array di variabili di impegnare

$bind_array = array(
    ':id_1' => '1', 
    ': id_array_of_unknown_size' => array('7','2','5',), 
); 

Anche la sua importante notare che nella mia situazione particolare all'ingresso array($bind_array) può o non può contenere una sottoarray per l'elemento bind. Potrebbe benissimo essere il seguente

select * from table1 
where id > :id_1 
and id != :id_2 

e

$bind_array = array(
    ':id_1' => '1', 
    ':id_2' => '5', 
); 
+1

puoi associare a IN in oci? Penso che in DOP non puoi legare a IN – JvdBerg

risposta

2

Uno è il legame di un piccolo numero fisso di valori in una clausola come descritto nei documenti di oci_bind_by_name. Esiste anche una soluzione per associare più condizioni con un numero variabile di valori.

<?php 
$ids = array(
    103, 
    104 
); 

$conn   = oci_pconnect($user, $pass, $tns); 
// Using ORACLE table() function to get the ids from the subquery 
$sql   = 'SELECT * FROM employees WHERE employee_id IN (SELECT column_value FROM table(:ids))'; 
$stmt   = oci_parse($conn, $sql); 
// Create collection of numbers. Build in type for strings is ODCIVARCHAR2LIST, but you can also create own types. 
$idCollection = oci_new_collection($conn, 'ODCINUMBERLIST', 'SYS'); 

// Maximum length of collections of type ODCINUMBERLIST is 32767, maybe you should check that! 
foreach ($ids as $id) { 
    $idCollection->append($id); 
} 

oci_bind_by_name($stmt, ':ids', $idCollection, -1, SQLT_NTY); 
oci_execute($stmt, OCI_DEFAULT); 
oci_fetch_all($stmt, $return); 
oci_free_statement($stmt); 

oci_close($conn); 

?> 
Problemi correlati