<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >mysql_fetch_assoc</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="MySQL 函数" HREF="ref.mysql.html"><LINK REL="PREVIOUS" TITLE="mysql_fetch_array" HREF="function.mysql-fetch-array.html"><LINK REL="NEXT" TITLE="mysql_fetch_field" HREF="function.mysql-fetch-field.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.mysql-fetch-array.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.mysql-fetch-field.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.mysql-fetch-assoc" ></A >mysql_fetch_assoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN135490" ></A ><P > (PHP 4 >= 4.0.3, PHP 5)</P >mysql_fetch_assoc -- 从结果集中取得一行作为关联数组 </DIV ><DIV CLASS="refsect1" ><A NAME="AEN135493" ></A ><H2 >说明</H2 >array <B CLASS="methodname" >mysql_fetch_assoc</B > ( resource result )<BR ></BR ><P > 返回根据从结果集取得的行生成的关联数组,如果没有更多行则返回 <TT CLASS="constant" ><B >FALSE</B ></TT >。 </P ><P > <B CLASS="function" >mysql_fetch_assoc()</B > 和用 <A HREF="function.mysql-fetch-array.html" ><B CLASS="function" >mysql_fetch_array()</B ></A > 加上第二个可选参数 MYSQL_ASSOC 完全相同。它仅仅返回关联数组。这也是 <A HREF="function.mysql-fetch-array.html" ><B CLASS="function" >mysql_fetch_array()</B ></A > 起初始的工作方式。如果在关联索引之外还需要数字索引,用 <A HREF="function.mysql-fetch-array.html" ><B CLASS="function" >mysql_fetch_array()</B ></A >。 </P ><P > 如果结果中的两个或以上的列具有相同字段名,最后一列将优先。要访问同名的其它列,要么用 <A HREF="function.mysql-fetch-row.html" ><B CLASS="function" >mysql_fetch_row()</B ></A > 来取得数字索引或给该列起个别名。参见 <A HREF="function.mysql-fetch-array.html" ><B CLASS="function" >mysql_fetch_array()</B ></A > 例子中有关别名说明。 </P ><P > 有一点很重要必须指出,用 <B CLASS="function" >mysql_fetch_assoc()</B > 并<SPAN CLASS="emphasis" ><I CLASS="emphasis" >不明显</I ></SPAN > 比用 <A HREF="function.mysql-fetch-row.html" ><B CLASS="function" >mysql_fetch_row()</B ></A > 慢,而且还提供了明显更多的值。 </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >本函数返回的字段名是<SPAN CLASS="emphasis" ><I CLASS="emphasis" >区分大小写</I ></SPAN >的。</P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN135519" ></A ><P ><B >例 1. 扩展的 <B CLASS="function" >mysql_fetch_assoc()</B > 例子</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br /> $conn </font><font color="#007700">= </font><font color="#0000BB">mysql_connect</font><font color="#007700">(</font><font color="#DD0000">"localhost"</font><font color="#007700">, </font><font color="#DD0000">"mysql_user"</font><font color="#007700">, </font><font color="#DD0000">"mysql_password"</font><font color="#007700">);<br /><br /> if (!</font><font color="#0000BB">$conn</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"Unable to connect to DB: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">();<br /> exit;<br /> }<br /><br /> if (!</font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"mydbname"</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"Unable to select mydbname: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">();<br /> exit;<br /> }<br /><br /> </font><font color="#0000BB">$sql </font><font color="#007700">= </font><font color="#DD0000">"SELECT id as userid, fullname, userstatus<br /> FROM sometable<br /> WHERE userstatus = 1"</font><font color="#007700">;<br /><br /> </font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#0000BB">$sql</font><font color="#007700">);<br /><br /> if (!</font><font color="#0000BB">$result</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"Could not successfully run query ($sql) from DB: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">();<br /> exit;<br /> }<br /><br /> if (</font><font color="#0000BB">mysql_num_rows</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">) == </font><font color="#0000BB">0</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"No rows found, nothing to print so am exiting"</font><font color="#007700">;<br /> exit;<br /> }<br /><br /> </font><font color="#FF8000">// While a row of data exists, put that row in $row as an associative array<br /> // Note: If you're expecting just one row, no need to use a loop<br /> // Note: If you put extract($row); inside the following loop, you'll<br /> // then create $userid, $fullname, and $userstatus<br /> </font><font color="#007700">while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_assoc</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">)) {<br /> echo </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"userid"</font><font color="#007700">];<br /> echo </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"fullname"</font><font color="#007700">];<br /> echo </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"userstatus"</font><font color="#007700">];<br /> }<br /><br /> </font><font color="#0000BB">mysql_free_result</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 参见 <A HREF="function.mysql-fetch-row.html" ><B CLASS="function" >mysql_fetch_row()</B ></A >,<A HREF="function.mysql-fetch-array.html" ><B CLASS="function" >mysql_fetch_array()</B ></A >,<A HREF="function.mysql-query.html" ><B CLASS="function" >mysql_query()</B ></A > 和 <A HREF="function.mysql-error.html" ><B CLASS="function" >mysql_error()</B ></A >。 </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.mysql-fetch-array.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.mysql-fetch-field.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >mysql_fetch_array</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.mysql.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >mysql_fetch_field</TD ></TR ></TABLE ></DIV ></BODY ></HTML >