<HTML ><HEAD ><TITLE >mysql_fetch_array</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="MySQL Functions" 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=ISO-8859-1"></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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.mysql-escape-string.html" ACCESSKEY="P" >Prev</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" >Next</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="AEN58610" ></A ><P > (PHP 3, PHP 4 )</P >mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN58613" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >mysql_fetch_array</B > ( resource result [, int result_type])<BR ></BR ><P > Returns an array that corresponds to the fetched row, or <TT CLASS="constant" ><B >FALSE</B ></TT > if there are no more rows.</P ><P > <B CLASS="function" >mysql_fetch_array()</B > is an extended version of <A HREF="function.mysql-fetch-row.html" ><B CLASS="function" >mysql_fetch_row()</B ></A >. In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys. </P ><P > If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name (by using <TT CLASS="literal" >'field'</TT > in this example). <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN58631" ></A ><P ><B >Example 1. Query with duplicate field names</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 > An important thing to note is that using <B CLASS="function" >mysql_fetch_array()</B > is <SPAN CLASS="emphasis" ><I CLASS="emphasis" >not significantly</I ></SPAN > slower than using <A HREF="function.mysql-fetch-row.html" ><B CLASS="function" >mysql_fetch_row()</B ></A >, while it provides a significant added value. </P ><P > The optional second argument <TT CLASS="parameter" ><I >result_type</I ></TT > in <B CLASS="function" >mysql_fetch_array()</B > is a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. This feature was added in PHP 3.0.7. MYSQL_BOTH is the default for this argument. </P ><P > By using MYSQL_BOTH, you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as <A HREF="function.mysql-fetch-assoc.html" ><B CLASS="function" >mysql_fetch_assoc()</B ></A > works), using MYSQL_NUM, you only get number indices (as <A HREF="function.mysql-fetch-row.html" ><B CLASS="function" >mysql_fetch_row()</B ></A > works). </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN58644" ></A ><P ><B >Example 2. mysql_fetch_array with MYSQL_NUM</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); mysql_select_db("mydb"); $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { printf ("ID: %s Name: %s", $row[0], $row[1]); } mysql_free_result($result); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN58647" ></A ><P ><B >Example 3. mysql_fetch_array with MYSQL_ASSOC</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); mysql_select_db("mydb"); $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf ("ID: %s Name: %s", $row["id"], $row["name"]); } mysql_free_result($result); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN58650" ></A ><P ><B >Example 4. mysql_fetch_array with MYSQL_BOTH</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); mysql_select_db("mydb"); $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { printf ("ID: %s Name: %s", $row[0], $row["name"]); } mysql_free_result($result); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > For further details, see also <A HREF="function.mysql-fetch-row.html" ><B CLASS="function" >mysql_fetch_row()</B ></A > and <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" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.mysql-fetch-assoc.html" ACCESSKEY="N" >Next</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" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >mysql_fetch_assoc</TD ></TR ></TABLE ></DIV ></BODY ></HTML >