php-doc-en/reference/mysql/functions/mysql-fetch-array.xml
Derick Rethans 01d05a8acf - Fix examples
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@83000 c90b9560-bf6c-de11-be94-00142212c4b1
2002-05-20 19:24:28 +00:00

153 lines
4.8 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.5 $ -->
<!-- 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 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: MYSQL_ASSOC, MYSQL_NUM, and
MYSQL_BOTH. This feature was added in PHP 3.0.7. MYSQL_BOTH
is the default for this argument.
</para>
<para>
By using MYSQL_BOTH, you'll get an array with both associative
and number indices. Using MYSQL_ASSOC, you only get associative
indices (as <function>mysql_fetch_assoc</function> works),
using MYSQL_NUM, you only get number indices (as
<function>mysql_fetch_row</function> works).
</para>
<example>
<title>mysql_fetch_array with MYSQL_NUM</title>
<programlisting role="php">
<![CDATA[
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("could not connect");
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>mysql_fetch_array with MYSQL_ASSOC</title>
<programlisting role="php">
<![CDATA[
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("could not connect");
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>mysql_fetch_array with MYSQL_BOTH</title>
<programlisting role="php">
<![CDATA[
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("could not connect");
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>
For further details, see also
<function>mysql_fetch_row</function> and
<function>mysql_fetch_assoc</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
-->