reference/mysqli/mysqli_result/fetch-object.xml
c5ccd084c8f830801a939bf1829ddddcaf019730
...
...
@@ -4,33 +4,46 @@
4
4
<refnamediv>
5
5
<refname>mysqli_result::fetch_object</refname>
6
6
<refname>mysqli_fetch_object</refname>
7
-
<refpurpose>Returns the current row of a result set as an object</refpurpose>
7
+
<refpurpose>Fetch the next row of a result set as an object</refpurpose>
8
8
</refnamediv>
9
9

10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
12
<para>&style.oop;</para>
13
-
<methodsynopsis role="oop">
14
-
<type>object</type><methodname>mysqli_result::fetch_object</methodname>
15
-
<methodparam choice="opt"><type>string</type><parameter>class_name</parameter><initializer>"stdClass"</initializer></methodparam>
16
-
<methodparam choice="opt"><type>array</type><parameter>params</parameter></methodparam>
13
+
<methodsynopsis role="mysqli_result">
14
+
<modifier>public</modifier> <type class="union"><type>object</type><type>null</type><type>false</type></type><methodname>mysqli_result::fetch_object</methodname>
15
+
<methodparam choice="opt"><type>string</type><parameter>class</parameter><initializer>"stdClass"</initializer></methodparam>
16
+
<methodparam choice="opt"><type>array</type><parameter>constructor_args</parameter><initializer>[]</initializer></methodparam>
17
17
</methodsynopsis>
18
18
<para>&style.procedural;</para>
19
19
<methodsynopsis>
20
-
<type>object</type><methodname>mysqli_fetch_object</methodname>
20
+
<type class="union"><type>object</type><type>null</type><type>false</type></type><methodname>mysqli_fetch_object</methodname>
21
21
<methodparam><type>mysqli_result</type><parameter>result</parameter></methodparam>
22
-
<methodparam choice="opt"><type>string</type><parameter>class_name</parameter><initializer>"stdClass"</initializer></methodparam>
23
-
<methodparam choice="opt"><type>array</type><parameter>params</parameter></methodparam>
22
+
<methodparam choice="opt"><type>string</type><parameter>class</parameter><initializer>"stdClass"</initializer></methodparam>
23
+
<methodparam choice="opt"><type>array</type><parameter>constructor_args</parameter><initializer>[]</initializer></methodparam>
24
24
</methodsynopsis>
25
25
<para>
26
-
The <function>mysqli_fetch_object</function> will return the current row
27
-
result set as an object where the attributes of the object represent the
28
-
names of the fields found within the result set.
26
+
Fetches one row of data from the result set and returns it as an object,
27
+
where each property represents the name of the result set's column.
28
+
Each subsequent call to this function will return the next row within the
29
+
result set, or &null; if there are no more rows.
29
30
</para>
30
31
<para>
31
-
Note that <function>mysqli_fetch_object</function> sets the properties
32
-
of the object before calling the object constructor.
32
+
If two or more columns of the result have the same name, the last
33
+
column will take precedence and overwrite any previous data. To
34
+
access multiple columns with the same name,
35
+
<function>mysqli_fetch_row</function> may be used to fetch the numerically
36
+
indexed array, or aliases may be used in the SQL query select list to give
37
+
columns different names.
33
38
</para>
39
+
<note xmlns="http://docbook.org/ns/docbook">
40
+
<simpara>
41
+
This function sets the properties
42
+
of the object before calling the object constructor.
43
+
</simpara>
44
+
</note>
45
+
&database.field-case;
46
+
&database.fetch-null;
34
47
</refsect1>
35
48

36
49
<refsect1 role="parameters">
...
...
@@ -39,7 +52,7 @@
39
52
<variablelist>
40
53
&mysqli.result.description;
41
54
<varlistentry>
42
-
<term><parameter>class_name</parameter></term>
55
+
<term><parameter>class</parameter></term>
43
56
<listitem>
44
57
<para>
45
58
The name of the class to instantiate, set the properties of and return.
...
...
@@ -48,11 +61,11 @@
48
61
</listitem>
49
62
</varlistentry>
50
63
<varlistentry>
51
-
<term><parameter>params</parameter></term>
64
+
<term><parameter>constructor_args</parameter></term>
52
65
<listitem>
53
66
<para>
54
67
An optional <type>array</type> of parameters to pass to the constructor
55
-
for <parameter>class_name</parameter> objects.
68
+
for <parameter>class</parameter> objects.
56
69
</para>
57
70
</listitem>
58
71
</varlistentry>
...
...
@@ -63,79 +76,90 @@
63
76
<refsect1 role="returnvalues">
64
77
&reftitle.returnvalues;
65
78
<para>
66
-
Returns an object with string properties that corresponds to the fetched
67
-
row or &null; if there are no more rows in resultset.
79
+
Returns an object representing the fetched row, where each property
80
+
represents the name of the result set's column, &null; if there
81
+
are no more rows in the result set, &return.falseforfailure;.
68
82
</para>
69
-
&database.field-case;
70
-
&database.fetch-null;
83
+
</refsect1>
84
+

85
+
<refsect1 role="errors">
86
+
&reftitle.errors;
87
+
<para>
88
+
A <classname>ValueError</classname> is thrown when
89
+
the <parameter>constructor_args</parameter> is non-empty with the class not having constructor.
90
+
</para>
91
+
</refsect1>
92
+

93
+
<refsect1 role="changelog">
94
+
&reftitle.changelog;
95
+
<informaltable>
96
+
<tgroup cols="2">
97
+
<thead>
98
+
<row>
99
+
<entry>&Version;</entry>
100
+
<entry>&Description;</entry>
101
+
</row>
102
+
</thead>
103
+
<tbody>
104
+
<row>
105
+
<entry>8.3.0</entry>
106
+
<entry>
107
+
Now throws a <classname>ValueError</classname> exception when
108
+
the <parameter>constructor_args</parameter> is non-empty with the class not having constructor;
109
+
previously an <classname>Exception</classname> was thrown.
110
+
</entry>
111
+
</row>
112
+
<row>
113
+
<entry>8.0.0</entry>
114
+
<entry>
115
+
<parameter>constructor_args</parameter> now accepts <literal>[]</literal> for constructors with 0 parameters;
116
+
previously an exception was thrown.
117
+
</entry>
118
+
</row>
119
+
</tbody>
120
+
</tgroup>
121
+
</informaltable>
71
122
</refsect1>
72
123

73
124
<refsect1 role="examples">
74
125
&reftitle.examples;
75
126
<example>
76
-
<title>&style.oop;</title>
127
+
<title><methodname>mysqli_result::fetch_object</methodname> example</title>
128
+
<para>&style.oop;</para>
77
129
<programlisting role="php">
78
130
<![CDATA[
79
131
<?php
80
-
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
81
132

82
-
/* check connection */
83
-
if (mysqli_connect_errno()) {
84
-
printf("Connect failed: %s\n", mysqli_connect_error());
85
-
exit();
86
-
}
87
-
88
-
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
133
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
134
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
89
135
90
-
if ($result = $mysqli->query($query)) {
136
+
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
91
137

92
-
/* fetch object array */
93
-
while ($obj = $result->fetch_object()) {
94
-
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
95
-
}
138
+
$result = $mysqli->query($query);
96
139

97
-
/* free result set */
98
-
$result->close();
140
+
while ($obj = $result->fetch_object()) {
141
+
printf("%s (%s)\n", $obj->Name, $obj->CountryCode);
99
142
}
100
-

101
-
/* close connection */
102
-
$mysqli->close();
103
-
?>
104
143
]]>
105
144
</programlisting>
106
-
</example>
107
-
<example>
108
-
<title>&style.procedural;</title>
145
+
<para>&style.procedural;</para>
109
146
<programlisting role="php">
110
147
<![CDATA[
111
148
<?php
112
-
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
113
149

114
-
/* check connection */
115
-
if (mysqli_connect_errno()) {
116
-
printf("Connect failed: %s\n", mysqli_connect_error());
117
-
exit();
118
-
}
119
-

120
-
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
150
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
151
+
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
121
152

122
-
if ($result = mysqli_query($link, $query)) {
153
+
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
123
154

124
-
/* fetch associative array */
125
-
while ($obj = mysqli_fetch_object($result)) {
126
-
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
127
-
}
155
+
$result = mysqli_query($link, $query);
128
156

129
-
/* free result set */
130
-
mysqli_free_result($result);
157
+
while ($obj = mysqli_fetch_object($result)) {
158
+
printf("%s (%s)\n", $obj->Name, $obj->CountryCode);
131
159
}
132
-

133
-
/* close connection */
134
-
mysqli_close($link);
135
-
?>
136
160
]]>
137
161
</programlisting>
138
-
&examples.outputs;
162
+
&examples.outputs.similar;
139
163
<screen>
140
164
<![CDATA[
141
165
Pueblo (USA)
...
...
@@ -154,6 +178,7 @@ Santa Clara (USA)
154
178
<simplelist>
155
179
<member><function>mysqli_fetch_array</function></member>
156
180
<member><function>mysqli_fetch_assoc</function></member>
181
+
<member><function>mysqli_fetch_column</function></member>
157
182
<member><function>mysqli_fetch_row</function></member>
158
183
<member><function>mysqli_query</function></member>
159
184
<member><function>mysqli_data_seek</function></member>
...
...
@@ -162,7 +187,6 @@ Santa Clara (USA)
162
187
</refsect1>
163
188

164
189
</refentry>
165
-

166
190
<!-- Keep this comment at the end of the file
167
191
Local variables:
168
192
mode: sgml
169
193