php-doc-en/reference/mysql/functions/mysql-fetch-array.xml
Damien Seguy c8b844ebea adding extra tags
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@169455 c90b9560-bf6c-de11-be94-00142212c4b1
2004-09-28 15:33:55 +00:00

168 lines
5.2 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.18 $ -->
<!-- splitted from ./en/functions/mysql.xml, last change in rev 1.27 -->
<refentry id="function.mysql-fetch-array">
<refnamediv>
<refname>mysql_fetch_array</refname>
<refpurpose>
Fetch a result row as an associative array, a numeric array, or both
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>mysql_fetch_array</methodname>
<methodparam><type>resource</type><parameter>result</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>
result_type
</parameter></methodparam>
</methodsynopsis>
<para>
Returns an array that corresponds to the fetched row, or &false;
if there are no more rows.</para>
<para>
<function>mysql_fetch_array</function> is an extended version of
<function>mysql_fetch_row</function>. 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.
</para>
<para>
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
<literal>'field'</literal> in this example).
<example>
<title>Query with aliased duplicate field names</title>
<programlisting role="sql">
<![CDATA[
SELECT table1.field AS foo, table2.field AS bar FROM table1, table2
]]>
</programlisting>
</example>
</para>
<para>
An important thing to note is that using
<function>mysql_fetch_array</function> is <emphasis>not
significantly</emphasis> slower than using
<function>mysql_fetch_row</function>, while it provides
a significant added value.
</para>
<para>
The optional second argument <parameter>result_type</parameter>
in <function>mysql_fetch_array</function> is a constant and can
take the following values: <constant>MYSQL_ASSOC</constant>,
<constant>MYSQL_NUM</constant>, and <constant>MYSQL_BOTH</constant>.
This feature was added in PHP 3.0.7. <constant>MYSQL_BOTH</constant>
is the default for this argument.
</para>
<para>
By using <constant>MYSQL_BOTH</constant>, you'll get an array
with both associative and number indices. Using
<constant>MYSQL_ASSOC</constant>, you only get associative
indices (as <function>mysql_fetch_assoc</function> works),
using <constant>MYSQL_NUM</constant>, you only get number indices (as
<function>mysql_fetch_row</function> works).
</para>
&database.field-case;
&database.fetch-null;
<para>
<example>
<title>
<function>mysql_fetch_array</function> with
<constant>MYSQL_NUM</constant>
</title>
<programlisting role="php">
<![CDATA[
<?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);
?>
]]>
</programlisting>
</example>
<example>
<title>
<function>mysql_fetch_array</function> with <constant>MYSQL_ASSOC</constant>
</title>
<programlisting role="php">
<![CDATA[
<?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);
?>
]]>
</programlisting>
</example>
<example>
<title>
<function>mysql_fetch_array</function> with <constant>MYSQL_BOTH</constant>
</title>
<programlisting role="php">
<![CDATA[
<?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);
?>
]]>
</programlisting>
</example>
</para>
<para>
See also
<function>mysql_fetch_row</function>,
<function>mysql_fetch_assoc</function>,
<function>mysql_data_seek</function> and
<function>mysql_query</function>.
</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:"../../../../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
-->