-
Notifications
You must be signed in to change notification settings - Fork 888
Expand file tree
/
Copy pathconstants.fetch-modes.xml
More file actions
1455 lines (1360 loc) · 36.8 KB
/
Copy pathconstants.fetch-modes.xml
File metadata and controls
1455 lines (1360 loc) · 36.8 KB
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<section xmlns="http://docbook.org/ns/docbook" xml:id="pdo.constants.fetch-modes">
<title>Fetch Modes</title>
<simpara>
See <link linkend="pdo.constants.cursors">cursor constants</link> for the
<literal>PDO::FETCH_ORI_*</literal> cursor constants.
</simpara>
<section xml:id="pdo.fetch-modes.basic" annotations="chunk:false">
<title>Basic Fetch Modes</title>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>Fetch Mode</entry>
<entry>Summary</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<constant>PDO::FETCH_DEFAULT</constant>
</entry>
<entry>
Special value for using the current default fetch mode.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_ASSOC</constant>
</entry>
<entry>
Array indexed by column name only.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_BOTH</constant> (Default)
</entry>
<entry>
Array indexed by both column number and name.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_NAMED</constant>
</entry>
<entry>
Variant of <constant>PDO::FETCH_ASSOC</constant> that retains duplicated
columns.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_NUM</constant>
</entry>
<entry>
Array indexed by column number only.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_COLUMN</constant>
</entry>
<entry>
A single column.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_KEY_PAIR</constant>
</entry>
<entry>
Key-value pairs, indexed by the first column.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_FUNC</constant>
</entry>
<entry>
Use a function to create the return value.
(<methodname>PDOStatement::fetchAll</methodname> only)
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_OBJ</constant>
</entry>
<entry>
Anonymous (<classname>stdClass</classname>) object.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_CLASS</constant>
</entry>
<entry>
An object of a specified class.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section xml:id="pdo.fetch-modes.class-flags" annotations="chunk:false">
<title>PDO::FETCH_CLASS options</title>
<simpara>
These modes are used to implement options when using
<constant>PDO::FETCH_CLASS</constant>.
</simpara>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>Fetch Mode</entry>
<entry>Summary</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<constant>PDO::FETCH_CLASSTYPE</constant>
</entry>
<entry>
Use the first column as the class name.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_PROPS_LATE</constant>
</entry>
<entry>
Call the constructor before setting properties.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_SERIALIZE</constant>
</entry>
<entry>
Use PHP serialized data. Deprecated as of PHP 8.1.0.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section xml:id="pdo.fetch-modes.single" annotations="chunk:false">
<title>Single Result Modes</title>
<simpara>
The following modes cannot be used with
<methodname>PDOStatement::fetchAll</methodname>.
</simpara>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>Fetch Mode</entry>
<entry>Summary</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<constant>PDO::FETCH_BOUND</constant>
</entry>
<entry>
Bind values to specified variables.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_INTO</constant>
</entry>
<entry>
Update an existing object.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_LAZY</constant>
</entry>
<entry>
Lazy fetch via <classname>PDORow</classname> for array- and object-like
access.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section xml:id="pdo.fetch-modes.grouped" annotations="chunk:false">
<title>
Special Behavior Flags for <methodname>PDOStatement::fetchAll</methodname>
</title>
<simpara>
The following special modes for multiple results only work with
<methodname>PDOStatement::fetchAll</methodname> and do not work with some other
fetch modes. Check the full documentation for details.
</simpara>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>Fetch Mode</entry>
<entry>Summary</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<constant>PDO::FETCH_GROUP</constant>
</entry>
<entry>
Results are grouped by the first column.
</entry>
</row>
<row>
<entry>
<constant>PDO::FETCH_UNIQUE</constant>
</entry>
<entry>
Results are (uniquely) indexed by the first column.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section xml:id="pdo.fetch-modes.duplicate-names" annotations="chunk:false">
<title>Handling of Duplicated Column Names</title>
<simpara>
It's possible for results to contain multiple columns that use the same name.
For example, when joining 2 tables that both contain a column with the same
name.
</simpara>
<simpara>
Because PHP structures such as arrays and objects don't support multiple keys
or properties that use the same name, the returned array or object will
contain only 1 of the values using the same name.
</simpara>
<simpara>
Which value is returned for a given duplicated name should be considered
undefined.
</simpara>
<simpara>
To avoid this issue, explicitly name columns using an alias. For example:
</simpara>
<informalexample>
<programlisting role="sql">
<![CDATA[
SELECT table1.created_at AS t1_created_at,
table2.created_at AS t2_created_at
FROM table1
JOIN table2 ON table1.table2id = table2.id
]]>
</programlisting>
</informalexample>
<simpara>
See also <constant>PDO::FETCH_NAMED</constant>,
<constant>PDO::ATTR_FETCH_TABLE_NAMES</constant> and
<constant>PDO::ATTR_FETCH_CATALOG_NAMES</constant>.
</simpara>
</section>
<section xml:id="pdo.fetch-modes.default" annotations="chunk:false">
<title>Setting the Default Fetch Mode</title>
<simpara>
It is possible to set default fetch mode for all queries using
<constant>PDO::ATTR_DEFAULT_FETCH_MODE</constant> with
<methodname>PDO::__construct</methodname> or
<methodname>PDO::setAttribute</methodname>.
</simpara>
<simpara>
The default fetch mode for a specific statement can be set using
<methodname>PDOStatement::setFetchMode</methodname>.
This affects reuse as a prepared statement and iteration (using
&foreach;).
</simpara>
<caution>
<simpara>
<methodname>PDOStatement::setAttribute</methodname> cannot be used to set the
default fetch mode. It only accepts driver specific attributes and silently
ignores attributes that are not recognized.
</simpara>
</caution>
</section>
<section xml:id="pdo.constants.fetch-default" annotations="chunk:false">
<title>PDO::FETCH_DEFAULT (<type>int</type>)</title>
<simpara>
Available as of PHP 8.0.7.
</simpara>
<simpara>
This is a special value that uses the current default fetch mode for a
<classname>PDOStatement</classname>. It's specifically useful as the default
value for method parameters when extending
<classname>PDOStatement</classname> for use with
<constant>PDO::ATTR_STATEMENT_CLASS</constant>.
</simpara>
<para>
This value cannot be used with
<constant>PDO::ATTR_DEFAULT_FETCH_MODE</constant>.
</para>
</section>
<section xml:id="pdo.constants.fetch-assoc" annotations="chunk:false">
<title>PDO::FETCH_ASSOC (<type>int</type>)</title>
<simpara>
<constant>PDO::FETCH_ASSOC</constant> returns an array indexed by column name
only.
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$stmt = $pdo->query("SELECT userid, name, country FROM users");
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
print_r($row);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[userid] => 104
[name] => Chris
[country] => Ukraine
)
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-both" annotations="chunk:false">
<title>PDO::FETCH_BOTH (<type>int</type>)</title>
<simpara>
This is the default fetch mode.
</simpara>
<simpara>
<constant>PDO::FETCH_BOTH</constant> returns an array indexed by both column
number and name. This means that every returned value is duplicated for each
result row.
</simpara>
<simpara>
The column number starts at 0 and is determined by the result column order in
the query, not (for example) the order columns are defined in the table.
</simpara>
<note>
<simpara>
Using the numeric column index is not recommended as this may change when
the query is changed, or when the table schema is changed when using
<literal>SELECT *</literal>.
</simpara>
</note>
<note>
<simpara>
The number of entries indexed by name may not match the number of entries
indexed by number in cases where multiple returned columns use the same
name.
</simpara>
</note>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$stmt = $pdo->query("SELECT userid, name, country FROM users");
$row = $stmt->fetch(\PDO::FETCH_BOTH);
print_r($row);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[id] => 104,
[0] => 104,
[name] => Chris,
[1] => Chris,
[country] => Ukraine,
[2] => Ukraine
)
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-named" annotations="chunk:false">
<title>PDO::FETCH_NAMED (<type>int</type>)</title>
<simpara>
<constant>PDO::FETCH_NAMED</constant> returns results in the same format as
<constant>PDO::FETCH_ASSOC</constant> except that where multiple columns use
the same name, all values are returned as a list.
</simpara>
<simpara>
For more information on handling of duplicated column names and alternatives,
see the <link linkend="pdo.fetch-modes.duplicate-names">handling of
duplicated names section</link> above.
</simpara>
<simpara>
The order in which duplicated values are returned should be considered
undefined. There's no way to tell where each value came from.
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$stmt = $pdo->query(
"SELECT users.*, referrer.name
FROM users
LEFT JOIN users AS referrer ON users.referred_by = referrer.userid
WHERE userid = 109"
);
$row = $stmt->fetch(\PDO::FETCH_NUM);
print_r($row);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[userid] => 109
[name] => Array
(
[0] => Toni
[1] => Chris
)
[country] => Germany
[referred_by] = 104
)
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-num" annotations="chunk:false">
<title>PDO::FETCH_NUM (<type>int</type>)</title>
<simpara>
<constant>PDO::FETCH_NUM</constant> returns an array indexed by column number
only. The column number starts at 0 and is determined by the result column order in
the query, not (for example) the order columns are defined in the table.
</simpara>
<note>
<simpara>
Using the numeric column index is not recommended as this may change when
the query is changed, or when the table schema is changed when using
<literal>SELECT *</literal>.
</simpara>
</note>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$stmt = $pdo->query("SELECT userid, name, country FROM users");
$row = $stmt->fetch(\PDO::FETCH_NUM);
print_r($row);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => 104
[1] => Chris
[2] => Ukraine
)
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-column" annotations="chunk:false">
<title>PDO::FETCH_COLUMN (<type>int</type>)</title>
<simpara>
<constant>PDO::FETCH_COLUMN</constant> returns values from a single column.
Use the second argument for <methodname>PDOStatement::setFetchMode</methodname>
or <methodname>PDOStatement::fetchAll</methodname> to specify which column is
returned.
</simpara>
<simpara>
If the specified column does not exist a <classname>ValueError</classname>
will be thrown.
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$stmt = $pdo->query("SELECT name, country FROM users LIMIT 3");
$row = $stmt->fetchAll(\PDO::FETCH_COLUMN);
print_r($row);
$stmt = $pdo->query("SELECT name, country FROM users LIMIT 3");
$row = $stmt->fetchAll(\PDO::FETCH_COLUMN, 1);
print_r($row);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => Chris
[1] => Jamie
[2] => Robin
)
Array
(
[0] => Ukraine
[1] => England
[2] => Germany
)
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-key-pair" annotations="chunk:false">
<title>PDO::FETCH_KEY_PAIR (<type>int</type>)</title>
<simpara>
<constant>PDO::FETCH_KEY_PAIR</constant> returns pairs of values, indexed by
the first column. The results must contain only 2 columns. This fetch mode
only makes sense with <methodname>PDOStatement::fetchAll</methodname>.
</simpara>
<note>
<simpara>
If the first column is not unique, values will be lost. Which value(s) are
lost should be considered undefined.
</simpara>
</note>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$stmt = $pdo->query("SELECT name, country FROM users LIMIT 3");
$row = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR);
print_r($row);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[Chris] => Ukraine
[Jamie] => England
[Robin] => Germany
)
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-func" annotations="chunk:false">
<title>PDO::FETCH_FUNC (<type>int</type>)</title>
<simpara>
Specify a function to create the returned value. This mode can only be used
with <methodname>PDOStatement::fetchAll</methodname>.
</simpara>
<simpara>
The function receives the values as parameters.
There's no way to retrieve the column name a given value was associated with.
It is crucial to ensure that the column order in the query matches the
order of parameters of the function.
</simpara>
<note>
<simpara>
The effects of <constant>PDO::FETCH_GROUP</constant> and
<constant>PDO::FETCH_UNIQUE</constant> are applied to results before the
function is called.
</simpara>
</note>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function valueCreator($col1, $col2, $col3)
{
return [
'col1' => $col1,
'col2' => strtoupper($col2),
'col3' => $col3,
'customKey' => 'customValue',
];
}
$stmt = $pdo->query("SELECT userid, name, country FROM users LIMIT 3");
$row = $stmt->fetchAll(\PDO::FETCH_FUNC, valueCreator(...));
print_r($row);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => Array
(
[col1] => 104
[col2] => SAM
[col3] => Ukraine
[customKey] => customValue
)
[1] => Array
(
[col1] => 105
[col2] => JAMIE
[col3] => England
[customKey] => customValue
)
[2] => Array
(
[col1] => 107
[col2] => ROBIN
[col3] => Germany
[customKey] => customValue
)
)
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-obj" annotations="chunk:false">
<title>PDO::FETCH_OBJ (<type>int</type>)</title>
<simpara>
<constant>PDO::FETCH_OBJ</constant> returns a <classname>stdClass</classname>
object.
</simpara>
<simpara>
See also <methodname>PDOStatement::fetchObject</methodname> and
<constant>PDO::FETCH_CLASS</constant>.
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$stmt = $pdo->query("SELECT userid, name, country FROM users");
$row = $stmt->fetch(\PDO::FETCH_OBJ);
print_r($row);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
stdClass Object
(
[userid] => 104
[name] => Chris
[country] => Ukraine
)
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-class" annotations="chunk:false">
<title>PDO::FETCH_CLASS (<type>int</type>)</title>
<simpara>
Returns an object of a specified class. For additional behaviors see the
<link linkend="pdo.fetch-modes.class-flags">option flags</link>.
</simpara>
<simpara>
If a property does not exist with the name of a returned column, it will be
dynamically declared. This behavior is deprecated and will cause an error
from PHP 9.0.
</simpara>
<simpara>
See also <methodname>PDOStatement::fetchObject</methodname>.
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class TestEntity
{
public $userid;
public $name;
public $country;
public $referred_by_userid;
public function __construct()
{
print "Constructor called with ". count(func_get_args()) ." args\n";
print "Properties set when constructor called? "
. (isset($this->name) ? 'Yes' : 'No') . "\n";
}
}
$stmt = $db->query(
"SELECT userid, name, country, referred_by_userid FROM users"
);
$stmt->setFetchMode(PDO::FETCH_CLASS, TestEntity::class);
$result = $stmt->fetch();
var_dump($result);
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Constructor called with 0 args
Properties set when constructor called? Yes
object(TestEntity)#3 (4) {
["userid"]=>
int(104)
["name"]=>
string(5) "Chris"
["country"]=>
string(7) "Ukraine"
["referred_by_userid"]=>
NULL
}
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-classtype" annotations="chunk:false">
<title>PDO::FETCH_CLASSTYPE (<type>int</type>)</title>
<simpara>
This fetch mode can only be used combined with
<constant>PDO::FETCH_CLASS</constant> (and
<link linkend="pdo.fetch-modes.class-flags">its other options</link>).
</simpara>
<simpara>
When this fetch mode is used, PDO will use the first returned column as the
name of the class to return.
</simpara>
<simpara>
If the specified class cannot be found, a <classname>stdClass</classname>
object will be returned, without warning or error.
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class TestEntity
{
public $userid;
public $name;
public $country;
public $referred_by_userid;
public function __construct()
{
print "Constructor called with ". count(func_get_args()) ." args\n";
print "Properties set when constructor called? "
. (isset($this->name) ? 'Yes' : 'No') . "\n";
}
}
$stmt = $db->query(
"SELECT 'TestEntity', userid, name, country, referred_by_userid FROM users"
);
$stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE);
$result = $stmt->fetch();
var_dump($result);
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Constructor called with 0 args
Properties set when constructor called? Yes
object(TestEntity)#3 (4) {
["userid"]=>
int(104)
["name"]=>
string(5) "Chris"
["country"]=>
string(7) "Ukraine"
["referred_by_userid"]=>
NULL
}
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-props-late" annotations="chunk:false">
<title>PDO::FETCH_PROPS_LATE (<type>int</type>)</title>
<simpara>
This fetch mode can only be used combined with
<constant>PDO::FETCH_CLASS</constant> (and
<link linkend="pdo.fetch-modes.class-flags">its other options</link>).
</simpara>
<simpara>
When this fetch mode is used, the constructor will be called before the
properties are set.
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class TestEntity
{
public $userid;
public $name;
public $country;
public $referred_by_userid;
public function __construct()
{
print "Constructor called with ". count(func_get_args()) ." args\n";
print "Properties set when constructor called? "
. (isset($this->name) ? 'Yes' : 'No') . "\n";
}
}
$stmt = $db->query(
"SELECT userid, name, country, referred_by_userid FROM users"
);
$stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, TestEntity::class);
$result = $stmt->fetch();
var_dump($result);
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Constructor called with 0 args
Properties set when constructor called? No
object(TestEntity)#3 (4) {
["userid"]=>
int(104)
["name"]=>
string(5) "Chris"
["country"]=>
string(7) "Ukraine"
["referred_by_userid"]=>
NULL
}
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-serialize" annotations="chunk:false">
<title>PDO::FETCH_SERIALIZE (<type>int</type>)</title>
&warn.deprecated.feature-8-1-0;
<simpara>
This fetch mode can only be used combined with
<constant>PDO::FETCH_CLASS</constant> (and
<link linkend="pdo.fetch-modes.class-flags">its other options</link>).
</simpara>
<simpara>
When this fetch mode is used, the specified class must be
<interfacename>Serializable</interfacename>.
</simpara>
<caution>
<simpara>
This feature does not support a string that contains a complete serialized
object (with <function>serialize</function>).
</simpara>
</caution>
<caution>
<simpara>
This fetch mode does not call the constructor.
</simpara>
</caution>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class TestEntity implements Serializable
{
public $userid;
public $name;
public $country;
public $referred_by_userid;
public function __construct()
{
print "Constructor called with " . count(func_get_args()) . " args\n";
print "Properties set when constructor called? "
. (isset($this->name) ? 'Yes' : 'No') . "\n";
}
public function serialize()
{
return join(
"|",
[$this->userid, $this->name, $this->country, $this->referred_by_userid]
);
}
public function unserialize(string $data)
{
$parts = explode("|", $data);
$this->userid = (int) $parts[0];
$this->name = $parts[1];
$this->country = $parts[2];
$refId = $parts[3];
$this->referred_by_userid = ($refId === "" ? null : (int) $refId);
}
}
print "Set up record (constructor called manually):\n";
$db->exec(
"CREATE TABLE serialize (
sdata TEXT
)"
);
$origObj = new TestEntity();
$origObj->userid = 200;
$origObj->name = 'Seri';
$origObj->country = 'Syria';
$origObj->referred_by_userid = null;
$insert = $db->prepare("INSERT INTO serialize (sdata) VALUES (:sdata)");
$insert->execute(['sdata' => $origObj->serialize()]);
print "\nRetrieve result:\n";
$query = "SELECT sdata FROM serialize";
$stmt = $db->query($query);
// NOTE: Constructor is never called!
$stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_SERIALIZE, TestEntity::class);
$result = $stmt->fetch();
var_dump($result);
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Deprecated: TestEntity implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in Standard input code on line 2
Set up record (constructor called manually):
Constructor called with 0 args
Properties set when constructor called? No
Retrieve result:
Deprecated: PDOStatement::setFetchMode(): The PDO::FETCH_SERIALIZE mode is deprecated in Standard input code on line 58
Deprecated: PDOStatement::fetch(): The PDO::FETCH_SERIALIZE mode is deprecated in Standard input code on line 59
object(TestEntity)#5 (4) {
["userid"]=>
int(200)
["name"]=>
string(4) "Seri"
["country"]=>
string(5) "Syria"
["referred_by_userid"]=>
NULL
}
]]>
</screen>
</informalexample>
</section>
<section xml:id="pdo.constants.fetch-bound" annotations="chunk:false">
<title>PDO::FETCH_BOUND (<type>int</type>)</title>
<simpara>
This fetch mode cannot be used with
<methodname>PDOStatement::fetchAll</methodname>.
</simpara>
<simpara>
This fetch mode does not directly return a result, but binds values to
variables specified with <methodname>PDOStatement::bindColumn</methodname>. The
called fetch method returns &true;.
</simpara>
<note>
<simpara>
When using prepared statements, to work correctly, variables must be bound
after the query is executed.
</simpara>
</note>
<programlisting role="php">
<![CDATA[