<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.mysql-fetch-assoc" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>mysql_fetch_assoc</refname>
  <refpurpose>Fetch a result row as an associative array</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
  <warning>
   &mysql.alternative.note;
   <simplelist role="alternatives">
    <member><function>mysqli_fetch_assoc</function></member>
    <member>
     <methodname>PDOStatement::fetch</methodname>
     with <parameter>mode</parameter> as <constant>PDO::FETCH_ASSOC</constant>
    </member>
   </simplelist>
  </warning>
 </refsynopsisdiv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>array</type><methodname>mysql_fetch_assoc</methodname>
   <methodparam><type>resource</type><parameter>result</parameter></methodparam>
  </methodsynopsis>
  <para>
   Returns an associative array that corresponds to the fetched row
   and moves the internal data pointer ahead.
   <function>mysql_fetch_assoc</function> is equivalent to calling
   <function>mysql_fetch_array</function> with MYSQL_ASSOC for the
   optional second parameter. It only returns an associative array.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    &mysql.result.description;
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   Returns an associative array of strings that corresponds to the fetched row, or
   &false; if there are no more rows.
  </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 either need to access the
   result with numeric indices by using
   <function>mysql_fetch_row</function> or add alias names.
   See the example at the <function>mysql_fetch_array</function>
   description about aliases.
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>An expanded <function>mysql_fetch_assoc</function> example</title>
    <programlisting role="php">
<![CDATA[
<?php

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT id as userid, fullname, userstatus
        FROM   sometable
        WHERE  userstatus = 1";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

mysql_free_result($result);

?>
]]>
    </programlisting>
   </example>
  </para>
 </refsect1>

 <refsect1 role="notes">
  &reftitle.notes;
  <note>
   <title>Performance</title>
   <para>
    An important thing to note is that using
    <function>mysql_fetch_assoc</function> is <emphasis>not
    significantly</emphasis> slower than using
    <function>mysql_fetch_row</function>, while it
    provides a significant added value.
   </para>
  </note>
  &database.field-case;
  &database.fetch-null;
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>mysql_fetch_row</function></member>
    <member><function>mysql_fetch_array</function></member>
    <member><function>mysql_data_seek</function></member>
    <member><function>mysql_query</function></member>
    <member><function>mysql_error</function></member>
   </simplelist>
  </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:"~/.phpdoc/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
-->
