reference/mysqli/mysqli_result/fetch-assoc.xml
035c126c0393fe154bac46e2c3c489ebadce48a5
...
...
@@ -4,24 +4,34 @@
4
4
<refnamediv>
5
5
<refname>mysqli_result::fetch_assoc</refname>
6
6
<refname>mysqli_fetch_assoc</refname>
7
-
<refpurpose>Fetch a result row as an associative array</refpurpose>
7
+
<refpurpose>Fetch the next row of a result set as an associative array</refpurpose>
8
8
</refnamediv>
9
9

10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
-
<para>Object oriented style (method):</para>
13
-
<methodsynopsis>
14
-
<type>array</type><methodname>mysqli_result::fetch_assoc</methodname>
15
-
<void />
12
+
<para>&style.oop;</para>
13
+
<methodsynopsis role="mysqli_result">
14
+
<modifier>public</modifier> <type class="union"><type>array</type><type>null</type><type>false</type></type><methodname>mysqli_result::fetch_assoc</methodname>
15
+
<void/>
16
16
</methodsynopsis>
17
-
<para>Procedural style:</para>
17
+
<para>&style.procedural;</para>
18
18
<methodsynopsis>
19
-
<type>array</type><methodname>mysqli_fetch_assoc</methodname>
19
+
<type class="union"><type>array</type><type>null</type><type>false</type></type><methodname>mysqli_fetch_assoc</methodname>
20
20
<methodparam><type>mysqli_result</type><parameter>result</parameter></methodparam>
21
21
</methodsynopsis>
22
22
<para>
23
-
Returns an associative array that corresponds to the fetched row or &null;
24
-
if there are no more rows.
23
+
Fetches one row of data from the result set and returns it as an associative
24
+
array.
25
+
Each subsequent call to this function will return the next row within the
26
+
result set, or &null; if there are no more rows.
27
+
</para>
28
+
<para>
29
+
If two or more columns of the result have the same name, the last
30
+
column will take precedence and overwrite any previous data. To
31
+
access multiple columns with the same name,
32
+
<function>mysqli_fetch_row</function> may be used to fetch the numerically
33
+
indexed array, or aliases may be used in the SQL query select list to give
34
+
columns different names.
25
35
</para>
26
36
&database.field-case;
27
37
&database.fetch-null;
...
...
@@ -39,94 +49,113 @@
39
49
<refsect1 role="returnvalues">
40
50
&reftitle.returnvalues;
41
51
<para>
42
-
Returns an associative array of strings representing the fetched row in the result
43
-
set, where each key in the array represents the name of one of the result
44
-
set's columns or &null; if there are no more rows in resultset.
45
-
</para>
46
-
<para>
47
-
If two or more columns of the result have the same field names, the last
48
-
column will take precedence. To access the other column(s) of the same
49
-
name, you either need to access the result with numeric indices by using
50
-
<function>mysqli_fetch_row</function> or add alias names.
52
+
Returns an associative array representing the fetched row,
53
+
where each key in the array represents the name of one of the result
54
+
set's columns, &null; if there
55
+
are no more rows in the result set, &return.falseforfailure;.
51
56
</para>
52
57
</refsect1>
53
58

54
59
<refsect1 role="examples">
55
60
&reftitle.examples;
56
61
<example>
57
-
<title>Object oriented style</title>
62
+
<title><methodname>mysqli_result::fetch_assoc</methodname> example</title>
63
+
<para>&style.oop;</para>
58
64
<programlisting role="php">
59
65
<![CDATA[
60
66
<?php
67
+

68
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
61
69
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
62
70

63
-
/* check connection */
64
-
if (mysqli_connect_errno()) {
65
-
printf("Connect failed: %s\n", mysqli_connect_error());
66
-
exit();
71
+
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
72
+

73
+
$result = $mysqli->query($query);
74
+

75
+
/* fetch associative array */
76
+
while ($row = $result->fetch_assoc()) {
77
+
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
67
78
}
79
+
]]>
80
+
</programlisting>
81
+
<para>&style.procedural;</para>
82
+
<programlisting role="php">
83
+
<![CDATA[
84
+
<?php
68
85

69
-
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
86
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
87
+
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");
70
88

71
-
if ($result = $mysqli->query($query)) {
89
+
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
72
90

73
-
/* fetch associative array */
74
-
while ($row = $result->fetch_assoc()) {
75
-
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
76
-
}
91
+
$result = mysqli_query($mysqli, $query);
77
92

78
-
/* free result set */
79
-
$result->close();
93
+
/* fetch associative array */
94
+
while ($row = mysqli_fetch_assoc($result)) {
95
+
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
80
96
}
81
-

82
-
/* close connection */
83
-
$mysqli->close();
84
-
?>
85
97
]]>
86
-
</programlisting>
98
+
</programlisting>
99
+
&examples.outputs.similar;
100
+
<screen>
101
+
<![CDATA[
102
+
Pueblo (USA)
103
+
Arvada (USA)
104
+
Cape Coral (USA)
105
+
Green Bay (USA)
106
+
Santa Clara (USA)
107
+
]]>
108
+
</screen>
87
109
</example>
88
-
<example>
89
-
<title>Procedural style</title>
110
+
<example xml:id="mysqli-result.example.iterator">
111
+
<title>Comparison of <classname>mysqli_result</classname> <classname>iterator</classname> and <methodname>mysqli_result::fetch_assoc</methodname> usage</title>
112
+
<para>
113
+
<classname>mysqli_result</classname> can be iterated using &foreach;.
114
+
The result set will always be iterated from the first row, regardless of the current position.
115
+
</para>
90
116
<programlisting role="php">
91
117
<![CDATA[
92
118
<?php
93
-
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
94
119

95
-
/* check connection */
96
-
if (mysqli_connect_errno()) {
97
-
printf("Connect failed: %s\n", mysqli_connect_error());
98
-
exit();
99
-
}
120
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
121
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
100
122

101
-
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
123
+
$query = 'SELECT Name, CountryCode FROM City ORDER BY ID DESC';
102
124

103
-
if ($result = mysqli_query($link, $query)) {
125
+
// Using iterators
126
+
$result = $mysqli->query($query);
127
+
foreach ($result as $row) {
128
+
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
129
+
}
104
130

105
-
/* fetch associative array */
106
-
while ($row = mysqli_fetch_assoc($result)) {
107
-
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
108
-
}
131
+
echo "\n==================\n";
109
132

110
-
/* free result set */
111
-
mysqli_free_result($result);
133
+
// Not using iterators
134
+
$result = $mysqli->query($query);
135
+
while ($row = $result->fetch_assoc()) {
136
+
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
112
137
}
113
-

114
-
/* close connection */
115
-
mysqli_close($link);
116
-
?>
117
138
]]>
118
139
</programlisting>
119
-
</example>
120
-
&example.outputs;
121
-
<screen>
140
+
&example.outputs.similar;
141
+
<screen>
122
142
<![CDATA[
123
143
Pueblo (USA)
124
144
Arvada (USA)
125
145
Cape Coral (USA)
126
146
Green Bay (USA)
127
147
Santa Clara (USA)
148
+

149
+
==================
150
+
Pueblo (USA)
151
+
Arvada (USA)
152
+
Cape Coral (USA)
153
+
Green Bay (USA)
154
+
Santa Clara (USA)
155
+

128
156
]]>
129
-
</screen>
157
+
</screen>
158
+
</example>
130
159
</refsect1>
131
160

132
161
<refsect1 role="seealso">
...
...
@@ -134,6 +163,7 @@ Santa Clara (USA)
134
163
<para>
135
164
<simplelist>
136
165
<member><function>mysqli_fetch_array</function></member>
166
+
<member><function>mysqli_fetch_column</function></member>
137
167
<member><function>mysqli_fetch_row</function></member>
138
168
<member><function>mysqli_fetch_object</function></member>
139
169
<member><function>mysqli_query</function></member>
...
...
@@ -143,7 +173,6 @@ Santa Clara (USA)
143
173
</refsect1>
144
174

145
175
</refentry>
146
-

147
176
<!-- Keep this comment at the end of the file
148
177
Local variables:
149
178
mode: sgml
150
179