/
mysql-fetch-assoc.xml
168 lines (150 loc) · 4.48 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"?>
<!-- $Revision$ -->
<!-- EN-Revision: ff4017b6334dae2d5353fe99df8089a828795324 Maintainer: HonestQiao Status: ready -->
<!-- CREDITS: Luffy -->
<refentry xml:id="function.mysql-fetch-assoc" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>mysql_fetch_assoc</refname>
<refpurpose>
从结果集中取得一行作为关联数组
</refpurpose>
</refnamediv>
<refsynopsisdiv>
<warning>
&mysql.alternative.note;
<simplelist role="alternatives">
<member><function>mysqli_fetch_assoc</function></member>
<member>
<methodname>PDOStatement::fetch</methodname>
with <parameter>mode</parameter> as <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>
返回对应结果集的关联数组,并且继续移动内部数据指针。
<function>mysql_fetch_assoc</function> 和用
<function>mysql_fetch_array</function> 加上第二个可选参数
MYSQL_ASSOC 完全相同。它仅仅返回关联数组。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
&mysql.result.description;
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回根据从结果集取得的行生成的关联数组;如果没有更多行则返回 &false;。
</para>
<para>
如果结果中的两个或以上的列具有相同字段名,最后一列将优先。要访问同名的其它列,要么用
<function>mysql_fetch_row</function>
来取得数字索引或给该列起个别名。
参见
<function>mysql_fetch_array</function> 例子中有关别名说明。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>扩展的 <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>性能</title>
<para>
必须指出一个要点:
<function>mysql_fetch_assoc</function> 比
<function>mysql_fetch_row</function>
并<emphasis>不明显</emphasis>
慢,而且还提供了更多有用的值。
</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
-->