-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathmysql-fetch-assoc.xml
168 lines (150 loc) · 4.62 KB
/
mysql-fetch-assoc.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
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: ff4017b6334dae2d5353fe99df8089a828795324 Maintainer: fernandoc Status: ready -->
<refentry xml:id="function.mysql-fetch-assoc" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>mysql_fetch_assoc</refname>
<refpurpose>Obtém uma linha do resultado como um array associativo</refpurpose>
</refnamediv>
<refsynopsisdiv>
<warning>
&mysql.alternative.note;
<simplelist role="alternatives">
<member><function>mysqli_fetch_assoc</function></member>
<member>
<methodname>PDOStatement::fetch</methodname>
com <parameter>mode</parameter> como <constant>PDO::FETCH_ASSOC</constant>
</member>
</simplelist>
</warning>
</refsynopsisdiv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>mysql_fetch_assoc</methodname>
<methodparam><type>resource</type><parameter>result</parameter></methodparam>
</methodsynopsis>
<para>
Retorna uma matriz associativa que corresponde a linha obtida
e move o ponteiro interno dos dados adiante.
<function>mysql_fetch_assoc</function> é equivalente a utilizar
<function>mysql_fetch_array</function> com MYSQL_ASSOC para o
segunto parâmetro, que é opcional. Ela apenas retorna um array associativo.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
&mysql.result.description;
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Retorna uma matriz associativa de strings que corresponde a linha obtida, ou
&false; se não houverem mais linhas.
</para>
<para>
Se duas ou mais colunas do resultado tiverem o mesmo nome de campo,
a ultima coluna terá precedencia. Para acessar as outras
coluna(s) com o mesmo nome, você precisa acessar os
outros resultados usando índices numéricos com
<function>mysql_fetch_row</function> ou adicionar alias (apelidos).
Veja o exemplo em <function>mysql_fetch_array</function>
para uma descrição sobre alias.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Um exemplo expandido de <function>mysql_fetch_assoc</function></title>
<programlisting role="php">
<![CDATA[
<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<title>Performance</title>
<para>
Uma coisa importante para notar é que usar
<function>mysql_fetch_assoc</function> <emphasis>não
é significativamente</emphasis> mais lento do que usar
<function>mysql_fetch_row</function>, enquanto provê
um valor agregado significante.
</para>
</note>
&database.field-case;
&database.fetch-null;
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mysql_fetch_row</function></member>
<member><function>mysql_fetch_array</function></member>
<member><function>mysql_data_seek</function></member>
<member><function>mysql_query</function></member>
<member><function>mysql_error</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
-->