2012-10-25 5 views
5

SQL IN 절에 대해 PHP OCI8을 사용하여 SQL을 바인딩 할 때 알 수없는 매개 변수를 처리하려면 어떻게해야합니까? 또한 입력 array($bind_array) 또는 내 특정 상황에서 점에 유의하는 것이 중요 포함하지 않을 수의PHP OCI8 'IN'구문에 대한 매개 변수를 알 수 없음

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

을 결합 할 수있는 다음과 같은 쿼리

select * from table1 
where id > :id_1 
and id in (:id_array_of_unknown_size) 

변수의 배열을 지정해, 예를 들어

, 바인드 요소의 서브 배열 oci_bind_array_by_name

http://php.net/manual/en/function.oci-bind-array-by-name - 그것은 단지뿐만 아니라

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

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

oci에서 IN에 바인드 할 수 있습니까? PDO에서는 IN에 바인딩 할 수 없다고 생각합니다. – JvdBerg

답변

2

한 가지 방법으로 배열 개체 변수는 oci_bind_by_name의 문서에 설명 된 것처럼 IN 절에있는 값의 작은 고정 된 수의 결합이다. 또한 여러 조건을 다양한 수의 값으로 바인딩하는 솔루션이 있습니다.

<?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); 

?>