加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

用于’IN’语句的PHP OCI8绑定(未知数量)参数

发布时间:2020-05-25 09:09:30 所属栏目:PHP 来源:互联网
导读:对于SQL IN子句,如何在使用 PHP OCI8绑定SQL时处理未知数量的参数? 例如,给出以下查询 select * from table1where id :id_1and id in (:id_array_of_unknown_size) 和要绑定的变量数组 $bind_array = array( :id_1 = 1, : id_array_of_un

对于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);

?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读