oci_bind_by_name

(PHP 5)

oci_bind_by_name -- 绑定一个 PHP 变量到一个 Oracle 位置标志符

说明

bool oci_bind_by_name ( resource stmt, string ph_name, mixed &variable [, int maxlength [, int type]] )

oci_bind_by_name() 将 PHP 变量 variable 绑定到 Oracle 的位置标志符 ph_name。该变量是否会被用作输入输出是在运行时决定的,并且函数给该变量分配必要的存储空间。length 参数确定该绑定的最大长度,如果将 length 设为 -1,oci_bind_by_name() 会用 variable 变量的当前长度确定绑定的最大长度。

如果要绑定一个抽象数据类型(LOB/ROWID/BFILE),需要先用 oci_new_descriptor() 函数分配空间。length 没有用于抽象数据类型,应被设为 -1。type 参数告诉 Oracle 要使用什么样的描述符。可能的值为:

  • OCI_B_FILE - 对应于 BFILE;

  • OCI_B_CFILE - 对应于 CFILE;

  • OCI_B_CLOB - 对应于 CLOB;

  • OCI_B_BLOB - 对应于 BLOB;

  • OCI_B_ROWID - 对应于 ROWID;

  • OCI_B_NTY - 对应于有名字的数据类型;

  • OCI_B_CURSOR - 对应于游标,是之前由 oci_new_cursor() 创建的。

例子 1. oci_bind_by_name() 例子

<?php
/* oci_bind_by_name example thies at thieso dot net (980221)
  inserts 3 records into emp, and uses the ROWID for updating the
  records just after the insert.
*/

$conn = oci_connect("scott", "tiger");

$stmt = oci_parse($conn, "
                          INSERT INTO
                                     emp (empno, ename)
                                              VALUES
                                     (:empno,:ename)
                            RETURNING
                                     ROWID
                                 INTO
                                     :rid
                                         "
);

$data = array(
              
1111 => "Larry",
              
2222 => "Bill",
              
3333 => "Jim"
             
);

$rowid = oci_new_descriptor($conn, OCI_D_ROWID);

oci_bind_by_name($stmt, ":empno", $empno, 32);
oci_bind_by_name($stmt, ":ename", $ename, 32);
oci_bind_by_name($stmt, ":rid",   $rowid, -1, OCI_B_ROWID);

$update = oci_parse($conn, "
                            UPDATE
                                  emp
                               SET
                                  sal = :sal
                             WHERE
                                  ROWID = :rid
                             "
);
oci_bind_by_name($update, ":rid", $rowid, -1, OCI_B_ROWID);
oci_bind_by_name($update, ":sal", $sal,   32);

$sal = 10000;

foreach (
$data as $empno => $ename) {
    
oci_execute($stmt);
    
oci_execute($update);
}

$rowid->free();

oci_free_statement($update);
oci_free_statement($stmt);

$stmt = oci_parse($conn, "
                          SELECT
                                *
                            FROM
                                emp
                           WHERE
                                empno
                              IN
                                (1111,2222,3333)
                              "
);
oci_execute($stmt);

while (
$row = oci_fetch_assoc($stmt)) {
    
var_dump($row);
}

oci_free_statement($stmt);

/* delete our "junk" from the emp table.... */
$stmt = oci_parse($conn, "
                          DELETE FROM
                                     emp
                                WHERE
                                     empno
                                   IN
                                     (1111,2222,3333)
                                   "
);
oci_execute($stmt);
oci_free_statement($stmt);

oci_close($conn);
?>

记住,本函数删除了行尾的空白字符。见以下例子:

例子 2. oci_bind_by_name() 例子

<?php
$connection
= oci_connect('apelsin','kanistra');
$query = "INSERT INTO test_table VALUES(:id, :text)";

$statement = oci_parse($query);
oci_bind_by_name($statement, ":id", 1);
oci_bind_by_name($statement, ":text", "trailing spaces follow     ");
oci_execute($statement);
/*
This code will insert into DB string 'trailing spaces follow', without
trailing spaces
*/
?>

例子 3. oci_bind_by_name() 例子

<?php
$connection
= oci_connect('apelsin','kanistra');
$query = "INSERT INTO test_table VALUES(:id, 'trailing spaces follow      ')";

$statement = oci_parse($query);
oci_bind_by_name($statement, ":id", 1);
oci_execute($statement);
/*
And this code will add 'trailing spaces follow      ', preserving
trailing whitespaces
*/
?>

警告

不要将 magic_quotes_gpcaddslashes()oci_bind_by_name() 同时使用,因为不需要转义,任何自动加上的引号都会被写入数据库中,因为 oci_bind_by_name() 不能分辨有意加上的引号和魔术引号。

如果成功则返回 TRUE,失败则返回 FALSE

注: 在 PHP 5.0.0 之前的版本必须使用 ocibindbyname() 替代本函数。该函数名仍然可用,为向下兼容作为 oci_bind_by_name() 的别名。不过其已被废弃,不推荐使用。


add a note add a note User Contributed Notes
js at mcs dot be
20-Feb-2006 08:48
Be careful that the variable argument is a reference. So, the following code does not work:
  foreach($some_array as $key => $value)
  {
   OCIBindByName($stmt, $key, $value);
  }

I assume this is because the contents of $value changes, even though the reference remains the same, so all bound variables end up pointing to the last loop iteration's value.

Instead use the following:
  foreach($some_array as $key => $value)
  {
   OCIBindByName($stmt, $key, $some_array[$key]);
  }
ehsmeng
17-Feb-2006 04:43
If you do a wrapper for these functions there is a bug I found on php5.1.1 / oracle 9 / windows xp. see the section with oci_bind_by_name.

function db_layer_insert_1_row ($dbquery, $bindvar = array())
{
   global $_db_layer_database;
   global $_db_layer_lasterror;
  
   if (false === ($stid = oci_parse($_db_layer_database, $dbquery))) {
       db_layer_lasterror ();
       return -1;
   }
  
   // Bind variables. NOTE substituting $bindvar[$bcol] with $bval causes
   // all variables to be set to the last value of $bval?!?!
   foreach ($bindvar as $bcol => $bval) {
       oci_bind_by_name($stid, $bcol, $bindvar[$bcol]); //$bval); <- bug?!
   }
  
   ...
hfuecks at nospam dot org
17-Aug-2005 04:12
Note that there have been some changes on the constant identifiers and the documentation is currently not entirely accurate.

Running the following script;

<?php
foreach (array_keys(get_defined_constants()) as $const) {
   if (
preg_match('/^OCI_B_/', $const) ) {
       print
"$const\n";
   }
}
?>

Under PHP 4.4.0 I get;

OCI_B_SQLT_NTY < renamed to OCI_B_NTY with PHP5
OCI_B_BFILE
OCI_B_CFILEE
OCI_B_CLOB
OCI_B_BLOB
OCI_B_ROWID
OCI_B_CURSOR
OCI_B_BIN

Under PHP 5.0.4 I get;

OCI_B_NTY
OCI_B_BFILE < docs are wrong right now
OCI_B_CFILEE < docs are wrong right now
OCI_B_CLOB
OCI_B_BLOB
OCI_B_ROWID
OCI_B_CURSOR
OCI_B_BIN < it's a mystery