<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >oci_bind_by_name</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Oracle 函数" HREF="ref.oci8.html"><LINK REL="PREVIOUS" TITLE="oci_bind_array_by_name" HREF="function.oci-bind-array-by-name.html"><LINK REL="NEXT" TITLE="oci_cancel" HREF="function.oci-cancel.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.oci-bind-array-by-name.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.oci-cancel.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.oci-bind-by-name" ></A >oci_bind_by_name</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN156656" ></A ><P > (PHP 5)</P >oci_bind_by_name -- 绑定一个 PHP 变量到一个 Oracle 位置标志符</DIV ><DIV CLASS="refsect1" ><A NAME="AEN156659" ></A ><H2 >说明</H2 >bool <B CLASS="methodname" >oci_bind_by_name</B > ( resource stmt, string ph_name, mixed &variable [, int maxlength [, int type]] )<BR ></BR ><P > <B CLASS="function" >oci_bind_by_name()</B > 将 PHP 变量 <CODE CLASS="parameter" >variable</CODE > 绑定到 Oracle 的位置标志符 <CODE CLASS="parameter" >ph_name</CODE >。该变量是否会被用作输入输出是在运行时决定的,并且函数给该变量分配必要的存储空间。<CODE CLASS="parameter" >length</CODE > 参数确定该绑定的最大长度,如果将 <CODE CLASS="parameter" >length</CODE > 设为 -1,<B CLASS="function" >oci_bind_by_name()</B > 会用 <CODE CLASS="parameter" >variable</CODE > 变量的当前长度确定绑定的最大长度。 </P ><P > 如果要绑定一个抽象数据类型(LOB/ROWID/BFILE),需要先用 <A HREF="function.oci-new-descriptor.html" ><B CLASS="function" >oci_new_descriptor()</B ></A > 函数分配空间。<CODE CLASS="parameter" >length</CODE > 没有用于抽象数据类型,应被设为 -1。<CODE CLASS="parameter" >type</CODE > 参数告诉 Oracle 要使用什么样的描述符。可能的值为: <P ></P ><UL ><LI ><P > <TT CLASS="constant" ><B >SQLT_FILE</B ></TT > - 对应于 BFILE; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_CFILE</B ></TT > - 对应于 CFILE; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_CLOB</B ></TT > - 对应于 CLOB; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_BLOB</B ></TT > - 对应于 BLOB; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_ROWID</B ></TT > - 对应于 ROWID; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_NTY</B ></TT > - 对应于有名字的数据类型; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_INT</B ></TT > - 对应于 integers; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_CHR</B ></TT > - 对应于 VARCHARs; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_BIN</B ></TT > - 对应于 RAW 列; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_LNG</B ></TT > - 对应于 LONG 列; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_LBI</B ></TT > - 对应于 LONG RAW 列; </P ></LI ><LI ><P > <TT CLASS="constant" ><B >SQLT_RSET</B ></TT > - 对应于游标,是之前由 <A HREF="function.oci-new-cursor.html" ><B CLASS="function" >oci_new_cursor()</B ></A > 创建的。 </P ></LI ></UL > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN156730" ></A ><P ><B >例 1. <B CLASS="function" >oci_bind_by_name()</B > 例子</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">/* oci_bind_by_name example thies at thieso dot net (980221)<br /> inserts 3 records into emp, and uses the ROWID for updating the<br /> records just after the insert.<br />*/<br /><br /></font><font color="#0000BB">$conn </font><font color="#007700">= </font><font color="#0000BB">oci_connect</font><font color="#007700">(</font><font color="#DD0000">"scott"</font><font color="#007700">, </font><font color="#DD0000">"tiger"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">oci_parse</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">, </font><font color="#DD0000">"<br /> INSERT INTO<br /> emp (empno, ename)<br /> VALUES<br /> (:empno,:ename)<br /> RETURNING<br /> ROWID<br /> INTO<br /> :rid<br /> "</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$data </font><font color="#007700">= array(<br /> </font><font color="#0000BB">1111 </font><font color="#007700">=> </font><font color="#DD0000">"Larry"</font><font color="#007700">,<br /> </font><font color="#0000BB">2222 </font><font color="#007700">=> </font><font color="#DD0000">"Bill"</font><font color="#007700">,<br /> </font><font color="#0000BB">3333 </font><font color="#007700">=> </font><font color="#DD0000">"Jim"<br /> </font><font color="#007700">);<br /><br /></font><font color="#0000BB">$rowid </font><font color="#007700">= </font><font color="#0000BB">oci_new_descriptor</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">, </font><font color="#0000BB">OCI_D_ROWID</font><font color="#007700">);<br /><br /></font><font color="#0000BB">oci_bind_by_name</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">, </font><font color="#DD0000">":empno"</font><font color="#007700">, </font><font color="#0000BB">$empno</font><font color="#007700">, </font><font color="#0000BB">32</font><font color="#007700">);<br /></font><font color="#0000BB">oci_bind_by_name</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">, </font><font color="#DD0000">":ename"</font><font color="#007700">, </font><font color="#0000BB">$ename</font><font color="#007700">, </font><font color="#0000BB">32</font><font color="#007700">);<br /></font><font color="#0000BB">oci_bind_by_name</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">, </font><font color="#DD0000">":rid"</font><font color="#007700">, </font><font color="#0000BB">$rowid</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">OCI_B_ROWID</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$update </font><font color="#007700">= </font><font color="#0000BB">oci_parse</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">, </font><font color="#DD0000">"<br /> UPDATE<br /> emp<br /> SET<br /> sal = :sal<br /> WHERE<br /> ROWID = :rid<br /> "</font><font color="#007700">);<br /></font><font color="#0000BB">oci_bind_by_name</font><font color="#007700">(</font><font color="#0000BB">$update</font><font color="#007700">, </font><font color="#DD0000">":rid"</font><font color="#007700">, </font><font color="#0000BB">$rowid</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">OCI_B_ROWID</font><font color="#007700">);<br /></font><font color="#0000BB">oci_bind_by_name</font><font color="#007700">(</font><font color="#0000BB">$update</font><font color="#007700">, </font><font color="#DD0000">":sal"</font><font color="#007700">, </font><font color="#0000BB">$sal</font><font color="#007700">, </font><font color="#0000BB">32</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$sal </font><font color="#007700">= </font><font color="#0000BB">10000</font><font color="#007700">;<br /><br />foreach (</font><font color="#0000BB">$data </font><font color="#007700">as </font><font color="#0000BB">$empno </font><font color="#007700">=> </font><font color="#0000BB">$ename</font><font color="#007700">) {<br /> </font><font color="#0000BB">oci_execute</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">);<br /> </font><font color="#0000BB">oci_execute</font><font color="#007700">(</font><font color="#0000BB">$update</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">$rowid</font><font color="#007700">-></font><font color="#0000BB">free</font><font color="#007700">();<br /><br /></font><font color="#0000BB">oci_free_statement</font><font color="#007700">(</font><font color="#0000BB">$update</font><font color="#007700">);<br /></font><font color="#0000BB">oci_free_statement</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">oci_parse</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">, </font><font color="#DD0000">"<br /> SELECT<br /> *<br /> FROM<br /> emp<br /> WHERE<br /> empno<br /> IN<br /> (1111,2222,3333)<br /> "</font><font color="#007700">);<br /></font><font color="#0000BB">oci_execute</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">);<br /><br />while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">oci_fetch_assoc</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">)) {<br /> </font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$row</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">oci_free_statement</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* delete our "junk" from the emp table.... */<br /></font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">oci_parse</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">, </font><font color="#DD0000">"<br /> DELETE FROM<br /> emp<br /> WHERE<br /> empno<br /> IN<br /> (1111,2222,3333)<br /> "</font><font color="#007700">);<br /></font><font color="#0000BB">oci_execute</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">);<br /></font><font color="#0000BB">oci_free_statement</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">);<br /><br /></font><font color="#0000BB">oci_close</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 记住,本函数删除了行尾的空白字符。见以下例子: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN156736" ></A ><P ><B >例 2. <B CLASS="function" >oci_bind_by_name()</B > 例子</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$connection </font><font color="#007700">= </font><font color="#0000BB">oci_connect</font><font color="#007700">(</font><font color="#DD0000">'apelsin'</font><font color="#007700">,</font><font color="#DD0000">'kanistra'</font><font color="#007700">);<br /></font><font color="#0000BB">$query </font><font color="#007700">= </font><font color="#DD0000">"INSERT INTO test_table VALUES(:id, :text)"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$statement </font><font color="#007700">= </font><font color="#0000BB">oci_parse</font><font color="#007700">(</font><font color="#0000BB">$query</font><font color="#007700">);<br /></font><font color="#0000BB">oci_bind_by_name</font><font color="#007700">(</font><font color="#0000BB">$statement</font><font color="#007700">, </font><font color="#DD0000">":id"</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br /></font><font color="#0000BB">oci_bind_by_name</font><font color="#007700">(</font><font color="#0000BB">$statement</font><font color="#007700">, </font><font color="#DD0000">":text"</font><font color="#007700">, </font><font color="#DD0000">"trailing spaces follow "</font><font color="#007700">);<br /></font><font color="#0000BB">oci_execute</font><font color="#007700">(</font><font color="#0000BB">$statement</font><font color="#007700">);<br /></font><font color="#FF8000">/*<br /> This code will insert into DB string 'trailing spaces follow', without<br /> trailing spaces<br />*/<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN156741" ></A ><P ><B >例 3. <B CLASS="function" >oci_bind_by_name()</B > 例子</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$connection </font><font color="#007700">= </font><font color="#0000BB">oci_connect</font><font color="#007700">(</font><font color="#DD0000">'apelsin'</font><font color="#007700">,</font><font color="#DD0000">'kanistra'</font><font color="#007700">);<br /></font><font color="#0000BB">$query </font><font color="#007700">= </font><font color="#DD0000">"INSERT INTO test_table VALUES(:id, 'trailing spaces follow ')"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$statement </font><font color="#007700">= </font><font color="#0000BB">oci_parse</font><font color="#007700">(</font><font color="#0000BB">$query</font><font color="#007700">);<br /></font><font color="#0000BB">oci_bind_by_name</font><font color="#007700">(</font><font color="#0000BB">$statement</font><font color="#007700">, </font><font color="#DD0000">":id"</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br /></font><font color="#0000BB">oci_execute</font><font color="#007700">(</font><font color="#0000BB">$statement</font><font color="#007700">);<br /></font><font color="#FF8000">/*<br /> And this code will add 'trailing spaces follow ', preserving<br /> trailing whitespaces<br />*/<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >警告</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > 不要将 <A HREF="ref.info.html#ini.magic-quotes-gpc" >magic_quotes_gpc</A > 或 <A HREF="function.addslashes.html" ><B CLASS="function" >addslashes()</B ></A > 与 <B CLASS="function" >oci_bind_by_name()</B > 同时使用,因为不需要转义,任何自动加上的引号都会被写入数据库中,因为 <B CLASS="function" >oci_bind_by_name()</B > 不能分辨有意加上的引号和魔术引号。 </P ></TD ></TR ></TABLE ></DIV ><P > 如果成功则返回 <TT CLASS="constant" ><B >TRUE</B ></TT >,失败则返回 <TT CLASS="constant" ><B >FALSE</B ></TT >。 </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 在 PHP 5.0.0 之前的版本必须使用 <A HREF="function.ocibindbyname.html" ><B CLASS="function" >ocibindbyname()</B ></A > 替代本函数。该函数名仍然可用,为向下兼容作为 <B CLASS="function" >oci_bind_by_name()</B > 的别名。不过其已被废弃,不推荐使用。 </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.oci-bind-array-by-name.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.oci-cancel.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >oci_bind_array_by_name</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.oci8.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >oci_cancel</TD ></TR ></TABLE ></DIV ></BODY ></HTML >