对于sql IN子句,如何在使用
PHP OCI8绑定sql时处理未知数量的参数?
例如,给出以下查询
select * from table1 where id > :id_1 and id in (:id_array_of_unkNown_size)
和要绑定的变量数组
$bind_array = array( ':id_1' => '1',': id_array_of_unkNown_size' => array('7','2','5',),);
另外需要注意的是,在我的特定情况下,输入数组($bind_array)可能包含也可能不包含bind元素的子数组.它也可以是以下
select * from table1 where id > :id_1 and id != :id_2
和
$bind_array = array( ':id_1' => '1',':id_2' => '5',);
一种方法是在IN子句中绑定少量固定数量的值,如
oci_bind_by_name的文档中所述.还有一种解决方案可以将多个条件与可变数量的值绑定.
<?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); ?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。