php-doc-en/reference/mysqli/functions/mysqli-fetch-array.xml

125 lines
4.2 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<refentry id="function.mysqli-fetch-array">
<refnamediv>
<refname>mysqli_fetch_array</refname>
<refpurpose>Fetch a result row as an associative, a numeric array, or both.</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>mysqli_fetch_array</methodname>
<methodparam><type>resource</type><parameter>result</parameter></methodparam>
<methodparam choice='opt'><type>int</type><parameter>resulttype</parameter></methodparam>
</methodsynopsis>
<para>
Returns an array that corresponds to the fetched row or &false; if there are no more rows for the
database connection represented by the <parameter>link</parameter> parameter.
</para>
<para>
<function>mysqli_fetch_array</function> is an extended version of the
<function>mysqli_fetch_row</function> function. In addition to storing the data in the
numeric indices of the result array, the <function>mysqli_fetch_array</function> function can
also store the data in associative indices, using the field names of the result set as keys.
</para>
<para>
If two or more columns of the result have the same field names, the last column will take
precedence and overwrite the earlier data. In order to access multiple columns with the same
name, the numerically indexed version of the row must be used.
</para>
<para>
The optional second argument <parameter>result_type</parameter> is a constant indicating what
type of array should be produced from the current row data. The possible values for this parameter
are the constants MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH. By default the
<function>mysqli_fetch_array</function> function will assume MYSQLI_BOTH for this parameter.
</para>
<para>
By using the MYSQLI_ASSOC constant this function will behave identically to the
<function>mysqli_fetch_assoc</function>, while MYSQLI_NUM will behave identically to the
<function>mysqli_fetch_row</function> function. The final option MYSQLI_BOTH will create a single
array with the attributes of both.
</para>
<example>
<title>mysqli_fetch_array with MYSQLI_NUM</title>
<programlisting role="php">
<![CDATA[
<?php
mysqli_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysqli_error());
mysqli_select_db("mydb");
$result = mysqli_query("SELECT id, name FROM mytable");
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
printf ("ID: %s Name: %s", $row[0], $row[1]);
}
mysqli_free_result($result);
?>
]]>
</programlisting>
</example>
<example>
<title>mysqli_fetch_array with MYSQLI_ASSOC</title>
<programlisting role="php">
<![CDATA[
<?php
mysqli_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysqli_error());
mysqli_select_db("mydb");
$result = mysqli_query("SELECT id, name FROM mytable");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
printf ("ID: %s Name: %s", $row["id"], $row["name"]);
}
mysqli_free_result($result);
?>
]]>
</programlisting>
</example>
<example>
<title>mysqli_fetch_array with MYSQLI_BOTH</title>
<programlisting role="php">
<![CDATA[
<?php
mysqli_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysqli_error());
mysqli_select_db("mydb");
$result = mysqli_query("SELECT id, name FROM mytable");
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}
mysqli_free_result($result);
?>
]]>
</programlisting>
</example>
</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
-->