1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.8 $ -->
<refentry id="function.oci-bind-by-name">
<refnamediv>
<refname>oci_bind_by_name</refname>
<refpurpose>
Binds the PHP variable to the Oracle placeholder
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>bool</type><methodname>oci_bind_by_name</methodname>
<methodparam><type>resource</type><parameter>stmt</parameter></methodparam>
<methodparam><type>string</type><parameter>ph_name</parameter></methodparam>
<methodparam><type>mixed</type><parameter role="reference">variable</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>maxlength</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>type</parameter></methodparam>
</methodsynopsis>
<para>
<function>oci_bind_by_name</function> binds the PHP variable
<parameter>variable</parameter> to the Oracle placeholder
<parameter>ph_name</parameter>. Whether it will be used for
input or output will be determined at run-time and the necessary
storage space will be allocated. The <parameter>length</parameter>
parameter sets the maximum length for the bind. If you set
<parameter>length</parameter> to -1
<function>oci_bind_by_name</function> will use the current length of
<parameter>variable</parameter> to set the maximum length.
</para>
<para>
If you need to bind an abstract datatype (LOB/ROWID/BFILE) you
need to allocate it first using the
<function>oci_new_descriptor</function> function. The
<parameter>length</parameter> is not used for abstract datatypes
and should be set to -1. The <parameter>type</parameter> parameter
tells Oracle which descriptor is used. Possible
values are:
<itemizedlist>
<listitem>
<para>
<constant>SQLT_FILE</constant> - for BFILEs;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_CFILE</constant> - for CFILEs;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_CLOB</constant> - for CLOBs;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_BLOB</constant> - for BLOBs;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_RDD</constant> - for ROWIDs;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_NTY</constant> - for named datatypes;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_INT</constant> - for integers;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_CHR</constant> - for VARCHARs;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_BIN</constant> - for RAW columns;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_LNG</constant> - for LONG columns;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_LBI</constant> - for LONG RAW columns;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_RSET</constant> - for cursors, that were created
before with <function>oci_new_cursor</function>.
</para>
</listitem>
</itemizedlist>
</para>
<para>
<example>
<title><function>oci_bind_by_name</function>example</title>
<programlisting role="php">
<![CDATA[
<?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);
?>
]]>
</programlisting>
</example>
</para>
<para>
Remember, this function strips trailing whitespaces. See the following
example:
</para>
<para>
<example>
<title><function>oci_bind_by_name</function> example</title>
<programlisting role="php">
<![CDATA[
<?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
*/
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title><function>oci_bind_by_name</function> example</title>
<programlisting role="php">
<![CDATA[
<?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
*/
?>
]]>
</programlisting>
</example>
</para>
<warning>
<para>
Do not use <link
linkend="ini.magic-quotes-gpc">magic_quotes_gpc</link> or
<function>addslashes</function> and <function>oci_bind_by_name</function>
simultaneously as no quoting is needed and any magically applied quotes
will be written into your database as <function>oci_bind_by_name</function>
is not able to distinguish magically added quotings from those added
intentionally.
</para>
</warning>
<para>
&return.success;
</para>
<note>
<para>
In PHP versions before 5.0.0 you must use <function>ocibindbyname</function> instead.
This name still can be used, it was left as alias of
<function>oci_bind_by_name</function> for downwards compatability.
This, however, is deprecated and not recommended.
</para>
</note>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|