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
12
<para>&style.oop;</para>
13
-
<methodsynopsis role="oop">
14
-
<type>array</type><methodname>mysqli_result::fetch_assoc</methodname>
15
-
<void />
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
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,84 +49,54 @@
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>&style.oop;</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
61
-
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
62
-

63
-
/* check connection */
64
-
if ($mysqli->connect_errno) {
65
-
printf("Connect failed: %s\n", $mysqli->connect_error);
66
-
exit();
67
-
}
68
67

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

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

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

78
-
/* free result set */
79
-
$result->free();
75
+
/* fetch associative array */
76
+
while ($row = $result->fetch_assoc()) {
77
+
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
80
78
}
81
-

82
-
/* close connection */
83
-
$mysqli->close();
84
-
?>
85
79
]]>
86
80
</programlisting>
87
-
</example>
88
-
<example>
89
-
<title>&style.procedural;</title>
81
+
<para>&style.procedural;</para>
90
82
<programlisting role="php">
91
83
<![CDATA[
92
84
<?php
93
-
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
94
85

95
-
/* check connection */
96
-
if (mysqli_connect_errno()) {
97
-
printf("Connect failed: %s\n", mysqli_connect_error());
98
-
exit();
99
-
}
100
-

101
-
$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");
102
88

103
-
if ($result = mysqli_query($link, $query)) {
89
+
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
104
90

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

110
-
/* free result set */
111
-
mysqli_free_result($result);
93
+
/* fetch associative array */
94
+
while ($row = mysqli_fetch_assoc($result)) {
95
+
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
112
96
}
113
-

114
-
/* close connection */
115
-
mysqli_close($link);
116
-
?>
117
97
]]>
118
98
</programlisting>
119
-
&examples.outputs;
99
+
&examples.outputs.similar;
120
100
<screen>
121
101
<![CDATA[
122
102
Pueblo (USA)
...
...
@@ -128,42 +108,51 @@ Santa Clara (USA)
128
108
</screen>
129
109
</example>
130
110
<example xml:id="mysqli-result.example.iterator">
131
-
<title>A <classname>mysqli_result</classname> example comparing <classname>iterator</classname> usage</title>
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>
132
116
<programlisting role="php">
133
117
<![CDATA[
134
118
<?php
135
-
$c = mysqli_connect('127.0.0.1','user', 'pass');
136
119

137
-
// Using iterators (support was added with PHP 5.4)
138
-
foreach ( $c->query('SELECT user,host FROM mysql.user') as $row ) {
139
-
printf("'%s'@'%s'\n", $row['user'], $row['host']);
120
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
121
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
122
+

123
+
$query = 'SELECT Name, CountryCode FROM City ORDER BY ID DESC';
124
+

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

142
131
echo "\n==================\n";
143
132

144
133
// Not using iterators
145
-
$result = $c->query('SELECT user,host FROM mysql.user');
134
+
$result = $mysqli->query($query);
146
135
while ($row = $result->fetch_assoc()) {
147
-
printf("'%s'@'%s'\n", $row['user'], $row['host']);
136
+
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
148
137
}
149
-

150
-
?>
151
138
]]>
152
139
</programlisting>
153
140
&example.outputs.similar;
154
141
<screen>
155
142
<![CDATA[
156
-
'root'@'192.168.1.1'
157
-
'root'@'127.0.0.1'
158
-
'dude'@'localhost'
159
-
'lebowski'@'localhost'
143
+
Pueblo (USA)
144
+
Arvada (USA)
145
+
Cape Coral (USA)
146
+
Green Bay (USA)
147
+
Santa Clara (USA)
160
148

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

163
-
'root'@'192.168.1.1'
164
-
'root'@'127.0.0.1'
165
-
'dude'@'localhost'
166
-
'lebowski'@'localhost'
167
156
]]>
168
157
</screen>
169
158
</example>
...
...
@@ -174,6 +163,7 @@ while ($row = $result->fetch_assoc()) {
174
163
<para>
175
164
<simplelist>
176
165
<member><function>mysqli_fetch_array</function></member>
166
+
<member><function>mysqli_fetch_column</function></member>
177
167
<member><function>mysqli_fetch_row</function></member>
178
168
<member><function>mysqli_fetch_object</function></member>
179
169
<member><function>mysqli_query</function></member>
...
...
@@ -183,7 +173,6 @@ while ($row = $result->fetch_assoc()) {
183
173
</refsect1>
184
174

185
175
</refentry>
186
-

187
176
<!-- Keep this comment at the end of the file
188
177
Local variables:
189
178
mode: sgml
190
179