-
Notifications
You must be signed in to change notification settings - Fork 102
/
oci-bind-by-name.xml
824 lines (703 loc) · 22.1 KB
/
oci-bind-by-name.xml
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 5e41012cfdf8f2eff5fa56de446c7656afac536c Maintainer: HonestQiao Status: ready -->
<!-- CREDITS: xuke, Gregory, dallas, mowangjuanzi -->
<refentry xml:id="function.oci-bind-by-name" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>oci_bind_by_name</refname>
<refpurpose>绑定 PHP 变量到 Oracle 位置标志符</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>bool</type><methodname>oci_bind_by_name</methodname>
<methodparam><type>resource</type><parameter>statement</parameter></methodparam>
<methodparam><type>string</type><parameter>param</parameter></methodparam>
<methodparam><type>mixed</type><parameter role="reference">var</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>max_length</parameter><initializer>-1</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>type</parameter><initializer>0</initializer></methodparam>
</methodsynopsis>
<para>
将 PHP 变量 <parameter>var</parameter> 绑定到 Oracle 位置占位符 <parameter>param</parameter>。绑定对
Oracle 数据库性能很重要,同时也是避免 SQL 注入安全问题的一种方式。
</para>
<para>
绑定允许数据库重用语句上下文和以前执行语句的缓存,即使另一个用户或进程最初(originally)执行它也是如此。绑定减少了 SQL
注入问题,因为与绑定变量关联的数据永远不会被视为 SQL 语句的一部分。不需要引用或转义。
</para>
<para>
已经绑定的 PHP 变量可以改变,语句重新执行,而不需要重新解析语句或重新绑定。
</para>
<para>
在 Oracle 中,绑定变量通常分为用于传递到数据库的值的 <literal>IN</literal> 绑定和用于返回给 PHP 的值的 <literal>OUT</literal>
绑定。绑定变量可以同时为 <literal>IN</literal> 和 <literal>OUT</literal>。绑定变量是用于输入还是输出是在运行时确定的。
</para>
<para>
使用 <literal>OUT</literal> 绑定时必须指定 <parameter>max_length</parameter> 以便 PHP 分配足够的内存来保存返回值。
</para>
<para>
对于 <literal>IN</literal> 绑定,如果使用不同的 PHP 变量值多次重新执行该语句,建议设置 <parameter>max_length</parameter> 长度。否则
Oracle 可能会将数据截断为初始 PHP 变量值的长度。如果不知道最大长度是多少,则在每次调用 <function>oci_execute</function> 之前使用当前数据大小重新调用
<function>oci_bind_by_name</function>。绑定不必要的大长度将对数据库中的进程内存产生影响。
</para>
<para>
绑定调用告诉 Oracle 从哪个内存地址读取数据。对于 <literal>IN</literal> 绑定,调用 <function>oci_execute</function>
时地址需要包含有效数据。这意味着变量绑定必须保留在作用域内直到执行。否则,可能会出现意外结果或错误,例如“ORA-01460:未实现或不合理的转换请求”。对于
<literal>OUT</literal> 绑定,一个问题是没有在 PHP 变量中设置值。
</para>
<para>
对于重复执行的语句,永远不变的绑定值可能会降低 Oracle 优化器选择最佳语句执行计划的能力。很少重新执行的长时间运行的语句可能无法从绑定中获益。但是,在这两种情况下,绑定可能比将字符串连接到
SQL 语句中更安全,因为如果连接未过滤的用户文本,这可能会带来安全风险。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>statement</parameter></term>
<listitem>
<para>
有效的 OCI8 语句标识符。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>param</parameter></term>
<listitem>
<para>
语句中使用以冒号为前缀的绑定变量占位符。冒号在 <parameter>param</parameter> 中是可选的。Oracle 不使用问号作为占位符。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>var</parameter></term>
<listitem>
<para>
与 <parameter>param</parameter> 关联的 PHP 变量
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>max_length</parameter></term>
<listitem>
<para>
设置数据的最大长度。如果将其设置为 -1,则此函数将使用 <parameter>var</parameter>
的当前长度来设置最大长度。在这种情况下,<parameter>var</parameter> 必须存在并且在调用
<function>oci_bind_by_name</function> 时包含数据。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>type</parameter></term>
<listitem>
<para>
Oracle 处理数据的数据类型。使用的默认 <parameter>type</parameter> 是 <constant>SQLT_CHR</constant>。可能的话,Oracle
将在此类型和数据库列(或 PL/SQL 变量类型)之间转换数据。
</para>
<para>
如果要绑定抽象数据类型(LOB/ROWID/BFILE),需要先用 <function>oci_new_descriptor</function>
函数分配空间。<parameter>length</parameter> 没有用于抽象数据类型,应被设为 -1。
</para>
<para>
<parameter>type</parameter> 的可能值为:
<itemizedlist>
<listitem>
<para>
<constant>SQLT_BFILEE</constant> 或 <constant>OCI_B_BFILE</constant>
<!-- really only single E --> 对应 BFILE;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_CFILEE</constant> 或 <constant>OCI_B_CFILEE</constant> 对应 CFILE;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_CLOB</constant> 或 <constant>OCI_B_CLOB</constant> 对应 CLOB;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_BLOB</constant> 或 <constant>OCI_B_BLOB</constant> 对应 BLOB;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_RDD</constant> 或 <constant>OCI_B_ROWID</constant> 对应 ROWID;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_NTY</constant> 或 <constant>OCI_B_NTY</constant> 对应命名数据类型;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_INT</constant> 或 <constant>OCI_B_INT</constant> 对应整型;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_CHR</constant> 对应 VARCHAR;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_BIN</constant> 或 <constant>OCI_B_BIN</constant>
对应 RAW 列;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_LNG</constant> 对应 LONG 列;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_LBI</constant> 对应 LONG RAW 列;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_RSET</constant> 对应使用 <function>oci_new_cursor</function> 创建的游标;
</para>
</listitem>
<listitem>
<para>
<constant>SQLT_BOL</constant> 或 <constant>OCI_B_BOL</constant> 对应 PL/SQL BOOLEAN(需要 Oracle Database 12c)
</para>
</listitem>
</itemizedlist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.success;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>使用 <function>oci_bind_by_name</function> 插入数据</title>
<programlisting role="php">
<![CDATA[
<?php
// Create the table with:
// CREATE TABLE mytab (id NUMBER, text VARCHAR2(40));
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
$stid = oci_parse($conn,"INSERT INTO mytab (id, text) VALUES(:id_bv, :text_bv)");
$id = 1;
$text = "Data to insert ";
oci_bind_by_name($stid, ":id_bv", $id);
oci_bind_by_name($stid, ":text_bv", $text);
oci_execute($stid);
// Table now contains: 1, 'Data to insert '
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>一次绑定多次执行</title>
<programlisting role="php">
<![CDATA[
<?php
// Create the table with:
// CREATE TABLE mytab (id NUMBER);
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
$a = array(1,3,5,7,11); // data to insert
$stid = oci_parse($conn, 'INSERT INTO mytab (id) VALUES (:bv)');
oci_bind_by_name($stid, ':bv', $v, 20);
foreach ($a as $v) {
$r = oci_execute($stid, OCI_DEFAULT); // don't auto commit
}
oci_commit($conn); // commit everything at once
// Table contains five rows: 1, 3, 5, 7, 11
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>使用 <literal>foreach</literal> 循环绑定</title>
<programlisting role="php">
<![CDATA[
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
$sql = 'SELECT * FROM departments WHERE department_name = :dname AND location_id = :loc';
$stid = oci_parse($conn, $sql);
$ba = array(':dname' => 'IT Support', ':loc' => 1700);
foreach ($ba as $key => $val) {
// oci_bind_by_name($stid, $key, $val) does not work
// because it binds each placeholder to the same location: $val
// instead use the actual location of the data: $ba[$key]
oci_bind_by_name($stid, $key, $ba[$key]);
}
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
foreach ($row as $item) {
print $item."<br>\n";
}
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>在 WHERE 子句中绑定</title>
<programlisting role="php">
<![CDATA[
<?php
$conn = oci_connect("hr", "hrpwd", "localhost/XE");
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
$sql = 'SELECT last_name FROM employees WHERE department_id = :didbv ORDER BY last_name';
$stid = oci_parse($conn, $sql);
$didbv = 60;
oci_bind_by_name($stid, ':didbv', $didbv);
oci_execute($stid);
while (($row = oci_fetch_array($stid, OCI_ASSOC)) != false) {
echo $row['LAST_NAME'] ."<br>\n";
}
// Output is
// Austin
// Ernst
// Hunold
// Lorentz
// Pataballa
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>使用 LIKE 子句绑定</title>
<programlisting role="php">
<![CDATA[
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
// Find all cities that begin with 'South'
$stid = oci_parse($conn, "SELECT city FROM locations WHERE city LIKE :bv");
$city = 'South%'; // '%' is a wildcard in SQL
oci_bind_by_name($stid, ":bv", $city);
oci_execute($stid);
oci_fetch_all($stid, $res);
foreach ($res['CITY'] as $c) {
print $c . "<br>\n";
}
// Output is
// South Brunswick
// South San Francisco
// Southlake
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>与 REGEXP_LIKE 绑定</title>
<programlisting role="php">
<![CDATA[
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
// Find all cities that contain 'ing'
$stid = oci_parse($conn, "SELECT city FROM locations WHERE REGEXP_LIKE(city, :bv)");
$city = '.*ing.*';
oci_bind_by_name($stid, ":bv", $city);
oci_execute($stid);
oci_fetch_all($stid, $res);
foreach ($res['CITY'] as $c) {
print $c . "<br>\n";
}
// Output is
// Beijing
// Singapore
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
对于少量固定数量的 IN 子句条件,请使用单独的绑定变量。运行时未知的值可以设置为 NULL。这允许所有应用程序用户使用单个语句,从而最大限度地提高 Oracle 数据库缓存效率。
</para>
<para>
<example>
<title>在 IN 子句中绑定多个值</title>
<programlisting role="php">
<![CDATA[
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
$sql = 'SELECT last_name FROM employees WHERE employee_id in (:e1, :e2, :e3)';
$stid = oci_parse($conn, $sql);
$mye1 = 103;
$mye2 = 104;
$mye3 = NULL; // pretend we were not given this value
oci_bind_by_name($stid, ':e1', $mye1);
oci_bind_by_name($stid, ':e2', $mye2);
oci_bind_by_name($stid, ':e3', $mye3);
oci_execute($stid);
oci_fetch_all($stid, $res);
foreach ($res['LAST_NAME'] as $name) {
print $name ."<br>\n";
}
// Output is
// Ernst
// Hunold
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Binding a ROWID returned by a query</title>
<programlisting role="php">
<![CDATA[
<?php
// Create the table with:
// CREATE TABLE mytab (id NUMBER, salary NUMBER, name VARCHAR2(40));
// INSERT INTO mytab (id, salary, name) VALUES (1, 100, 'Chris');
// COMMIT;
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT ROWID, name FROM mytab WHERE id = :id_bv FOR UPDATE');
$id = 1;
oci_bind_by_name($stid, ':id_bv', $id);
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
$rid = $row['ROWID'];
$name = $row['NAME'];
// Change name to upper case & save the changes
$name = strtoupper($name);
$stid = oci_parse($conn, 'UPDATE mytab SET name = :n_bv WHERE ROWID = :r_bv');
oci_bind_by_name($stid, ':n_bv', $name);
oci_bind_by_name($stid, ':r_bv', $rid, -1, OCI_B_ROWID);
oci_execute($stid);
// The table now contains 1, 100, CHRIS
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>INSERT 时绑定 ROWID</title>
<programlisting role="php">
<![CDATA[
<?php
// This example inserts an id & name, and then updates the salary
// Create the table with:
// CREATE TABLE mytab (id NUMBER, salary NUMBER, name VARCHAR2(40));
//
// Based on original ROWID example by thies at thieso dot net (980221)
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}
$sql = "INSERT INTO mytab (id, name) VALUES(:id_bv, :name_bv)
RETURNING ROWID INTO :rid";
$ins_stid = oci_parse($conn, $sql);
$rowid = oci_new_descriptor($conn, OCI_D_ROWID);
oci_bind_by_name($ins_stid, ":id_bv", $id, 10);
oci_bind_by_name($ins_stid, ":name_bv", $name, 32);
oci_bind_by_name($ins_stid, ":rid", $rowid, -1, OCI_B_ROWID);
$sql = "UPDATE mytab SET salary = :salary WHERE ROWID = :rid";
$upd_stid = oci_parse($conn, $sql);
oci_bind_by_name($upd_stid, ":rid", $rowid, -1, OCI_B_ROWID);
oci_bind_by_name($upd_stid, ":salary", $salary, 32);
// ids and names to insert
$data = array(1111 => "Larry",
2222 => "Bill",
3333 => "Jim");
// Salary of each person
$salary = 10000;
// Insert and immediately update each row
foreach ($data as $id => $name) {
oci_execute($ins_stid);
oci_execute($upd_stid);
}
$rowid->free();
oci_free_statement($upd_stid);
oci_free_statement($ins_stid);
// Show the new rows
$stid = oci_parse($conn, "SELECT * FROM mytab");
oci_execute($stid);
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
var_dump($row);
}
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>为 PL/SQL 存储函数绑定</title>
<programlisting role="php">
<![CDATA[
<?php
// Before running the PHP program, create a stored function in
// SQL*Plus or SQL Developer:
//
// CREATE OR REPLACE FUNCTION myfunc(p IN NUMBER) RETURN NUMBER AS
// BEGIN
// RETURN p * 3;
// END;
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$p = 8;
$stid = oci_parse($conn, 'begin :r := myfunc(:p); end;');
oci_bind_by_name($stid, ':p', $p);
// The return value is an OUT bind. The default type will be a string
// type so binding a length 40 means that at most 40 digits will be
// returned.
oci_bind_by_name($stid, ':r', $r, 40);
oci_execute($stid);
print "$r\n"; // prints 24
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>为 PL/SQL 存储过程绑定参数</title>
<programlisting role="php">
<![CDATA[
<?php
// Before running the PHP program, create a stored procedure in
// SQL*Plus or SQL Developer:
//
// CREATE OR REPLACE PROCEDURE myproc(p1 IN NUMBER, p2 OUT NUMBER) AS
// BEGIN
// p2 := p1 * 2;
// END;
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$p1 = 8;
$stid = oci_parse($conn, 'begin myproc(:p1, :p2); end;');
oci_bind_by_name($stid, ':p1', $p1);
// The second procedure parameter is an OUT bind. The default type
// will be a string type so binding a length 40 means that at most 40
// digits will be returned.
oci_bind_by_name($stid, ':p2', $p2, 40);
oci_execute($stid);
print "$p2\n"; // prints 16
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>绑定 CLOB 列</title>
<programlisting role="php">
<![CDATA[
<?php
// Before running, create the table:
// CREATE TABLE mytab (mykey NUMBER, myclob CLOB);
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$mykey = 12343; // arbitrary key for this example;
$sql = "INSERT INTO mytab (mykey, myclob)
VALUES (:mykey, EMPTY_CLOB())
RETURNING myclob INTO :myclob";
$stid = oci_parse($conn, $sql);
$clob = oci_new_descriptor($conn, OCI_D_LOB);
oci_bind_by_name($stid, ":mykey", $mykey, 5);
oci_bind_by_name($stid, ":myclob", $clob, -1, OCI_B_CLOB);
oci_execute($stid, OCI_DEFAULT);
$clob->save("A very long string");
oci_commit($conn);
// Fetching CLOB data
$query = 'SELECT myclob FROM mytab WHERE mykey = :mykey';
$stid = oci_parse ($conn, $query);
oci_bind_by_name($stid, ":mykey", $mykey, 5);
oci_execute($stid);
print '<table border="1">';
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_LOBS)) {
print '<tr><td>'.$row['MYCLOB'].'</td></tr>';
// In a loop, freeing the large variable before the 2nd fetch reduces PHP's peak memory usage
unset($row);
}
print '</table>';
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>绑定 PL/SQL BOOLEAN</title>
<programlisting role="php">
<![CDATA[
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$plsql =
"begin
:output1 := true;
:output2 := false;
end;";
$s = oci_parse($c, $plsql);
oci_bind_by_name($s, ':output1', $output1, -1, OCI_B_BOL);
oci_bind_by_name($s, ':output2', $output2, -1, OCI_B_BOL);
oci_execute($s);
var_dump($output1); // true
var_dump($output2); // false
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<warning>
<para>
不要同时使用 <function>addslashes</function> 和 <function>oci_bind_by_name</function>,因为不需要引号。任何应用的引号都将写入数据库,因为
<function>oci_bind_by_name</function> 逐字插入数据并且不会删除引号或转义字符。
</para>
</warning>
<note>
<para>
如果将字符串绑定到 <literal>WHERE</literal> 子句中的 <literal>CHAR</literal> 列,请记住 Oracle 对 <literal>CHAR</literal>
列使用空白填充的比较语义。PHP 变量使用空格填充到与成功的 <literal>WHERE</literal> 子句的列相同的宽度。
</para>
</note>
<note>
<para>
PHP <parameter>var</parameter> 参数是引用。某些形式的循环无法按预期工作:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
foreach ($myarray as $key => $value) {
oci_bind_by_name($stid, $key, $value);
}
?>
]]>
</programlisting>
</informalexample>
<para>
这会将每个键绑定到 $value 的位置,因此所有绑定变量最终都指向最后一个循环迭代的值。应该使用以下内容:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
foreach ($myarray as $key => $value) {
oci_bind_by_name($stid, $key, $myarray[$key]);
}
?>
]]>
</programlisting>
</informalexample>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>oci_bind_array_by_name</function></member>
<member><function>oci_parse</function></member>
</simplelist>
</para>
</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:"~/.phpdoc/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
-->