<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.5 $ -->
<!-- splitted from ./en/functions/mysql.xml, last change in rev 1.45 -->
  <refentry id="function.mysql-fetch-assoc">
   <refnamediv>
    <refname>mysql_fetch_assoc</refname>
    <refpurpose>
     Fetch a result row as an associative array
    </refpurpose>
   </refnamediv>
   <refsect1>
    <title>Description</title>
     <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,
     or &false; if there are no more rows.</para>
    <para>
     <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.
     This is the way <function>mysql_fetch_array</function> originally
     worked.  If you need the numeric indices as well as the
     associative, use <function>mysql_fetch_array</function>.
    </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>
    <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>
    <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>
     See also 
     <function>mysql_fetch_row</function>,
     <function>mysql_fetch_array</function>,
     <function>mysql_query</function>, and
     <function>mysql_error</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
-->