<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >mysql_fetch_array</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_escape_string" HREF="function.mysql-escape-string.html"><LINK REL="NEXT" TITLE="mysql_fetch_assoc" HREF="function.mysql-fetch-assoc.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-escape-string.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-assoc.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.mysql-fetch-array" ></A >mysql_fetch_array</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN135437" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >mysql_fetch_array -- 从结果集中取得一行作为关联数组,或数字数组,或二者兼有 </DIV ><DIV CLASS="refsect1" ><A NAME="AEN135440" ></A ><H2 >说明</H2 >array <B CLASS="methodname" >mysql_fetch_array</B > ( resource result [, int result_type] )<BR ></BR ><P > 返回根据从结果集取得的行生成的数组,如果没有更多行则返回 <TT CLASS="constant" ><B >FALSE</B ></TT >。 </P ><P > <B CLASS="function" >mysql_fetch_array()</B > 是 <A HREF="function.mysql-fetch-row.html" ><B CLASS="function" >mysql_fetch_row()</B ></A > 的扩展版本。除了将数据以数字索引方式储存在数组中之外,还可以将数据作为关联索引储存,用字段名作为键名。 </P ><P > 如果结果中的两个或以上的列具有相同字段名,最后一列将优先。要访问同名的其它列,必须用该列的数字索引或给该列起个别名。对有别名的列,不能再用原来的列名访问其内容(本例中的 <TT CLASS="literal" >'field'</TT >)。 <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN135458" ></A ><P ><B >例 1. 相同字段名的查询</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="sql" >select table1.field as foo, table2.field as bar from table1, table2</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 有一点很重要必须指出,用 <B CLASS="function" >mysql_fetch_array()</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 ><P > <B CLASS="function" >mysql_fetch_array()</B > 中可选的第二个参数 <CODE CLASS="parameter" >result_type</CODE > 是一个常量,可以接受以下值:MYSQL_ASSOC,MYSQL_NUM 和 MYSQL_BOTH。本特性是 PHP 3.0.7 起新加的。本参数的默认值是 MYSQL_BOTH。 </P ><P > 如果用了 MYSQL_BOTH,将得到一个同时包含关联和数字索引的数组。用 MYSQL_ASSOC 只得到关联索引(如同 <A HREF="function.mysql-fetch-assoc.html" ><B CLASS="function" >mysql_fetch_assoc()</B ></A > 那样),用 MYSQL_NUM 只得到数字索引(如同 <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="AEN135475" ></A ><P ><B >例 2. mysql_fetch_array 使用 MYSQL_NUM</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /> 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">) or<br /> die(</font><font color="#DD0000">"Could not connect: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br /> </font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"mydb"</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="#DD0000">"SELECT id, name FROM mytable"</font><font color="#007700">);<br /><br /> while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_array</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">, </font><font color="#0000BB">MYSQL_NUM</font><font color="#007700">)) {<br /> </font><font color="#0000BB">printf </font><font color="#007700">(</font><font color="#DD0000">"ID: %s Name: %s"</font><font color="#007700">, </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">1</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 /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN135479" ></A ><P ><B >例 3. mysql_fetch_array 使用 MYSQL_ASSOC</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /> 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">) or<br /> die(</font><font color="#DD0000">"Could not connect: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br /> </font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"mydb"</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="#DD0000">"SELECT id, name FROM mytable"</font><font color="#007700">);<br /><br /> while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_array</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">, </font><font color="#0000BB">MYSQL_ASSOC</font><font color="#007700">)) {<br /> </font><font color="#0000BB">printf </font><font color="#007700">(</font><font color="#DD0000">"ID: %s Name: %s"</font><font color="#007700">, </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"id"</font><font color="#007700">], </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"name"</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 /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN135483" ></A ><P ><B >例 4. mysql_fetch_array 使用 MYSQL_BOTH</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /> 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">) or<br /> die(</font><font color="#DD0000">"Could not connect: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br /> </font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"mydb"</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="#DD0000">"SELECT id, name FROM mytable"</font><font color="#007700">);<br /><br /> while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_array</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">, </font><font color="#0000BB">MYSQL_BOTH</font><font color="#007700">)) {<br /> </font><font color="#0000BB">printf </font><font color="#007700">(</font><font color="#DD0000">"ID: %s Name: %s"</font><font color="#007700">, </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"name"</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 /></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-assoc.html" ><B CLASS="function" >mysql_fetch_assoc()</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-escape-string.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-assoc.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >mysql_escape_string</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_assoc</TD ></TR ></TABLE ></DIV ></BODY ></HTML >