<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.mysql.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.mysql-fetch-assoc.php',
    1 => 'mysql_fetch_assoc',
    2 => 'Fetch a result row as an associative array',
  ),
  'up' => 
  array (
    0 => 'ref.mysql.php',
    1 => 'MySQL Functions',
  ),
  'prev' => 
  array (
    0 => 'function.mysql-fetch-array.php',
    1 => 'mysql_fetch_array',
  ),
  'next' => 
  array (
    0 => 'function.mysql-fetch-field.php',
    1 => 'mysql_fetch_field',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mysql/functions/mysql-fetch-assoc.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.mysql-fetch-assoc" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mysql_fetch_assoc</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.3, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_fetch_assoc</span> &mdash; <span class="dc-title">Fetch a result row as an associative array</span></p>

 </div>

 <div id="function.mysql-fetch-assoc-refsynopsisdiv">
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
Instead, the <a href="book.mysqli.php" class="link">MySQLi</a> or <a href="ref.pdo-mysql.php" class="link">PDO_MySQL</a> extension should be used.
See also <a href="mysqlinfo.api.choosing.php" class="link">MySQL: choosing an API</a> guide.
Alternatives to this function include:</p>
   <ul class="simplelist">
    <li><span class="function"><a href="mysqli-result.fetch-assoc.php" class="function">mysqli_fetch_assoc()</a></span></li>
    <li>
     <span class="methodname"><a href="pdostatement.fetch.php" class="methodname">PDOStatement::fetch()</a></span>
     with <code class="parameter">mode</code> as <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-assoc">PDO::FETCH_ASSOC</a></code></strong>
    </li>
   </ul>
  </div>
 </div>

 <div class="refsect1 description" id="refsect1-function.mysql-fetch-assoc-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mysql_fetch_assoc</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$result</code></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="simpara">
   Returns an associative array that corresponds to the fetched row
   and moves the internal data pointer ahead.
   <span class="function"><strong>mysql_fetch_assoc()</strong></span> is equivalent to calling
   <span class="function"><a href="function.mysql-fetch-array.php" class="function">mysql_fetch_array()</a></span> with MYSQL_ASSOC for the
   optional second parameter. It only returns an associative array.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mysql-fetch-assoc-parameters">
  <h3 class="title">Parameters</h3>
  <dl>
   <dt>
<code class="parameter">result</code></dt><dd><p class="para">The result <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> that
is being evaluated. This result comes from a call to
<span class="function"><a href="function.mysql-query.php" class="function">mysql_query()</a></span>.</p></dd>
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-fetch-assoc-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="simpara">
   Returns an associative array of strings that corresponds to the fetched row, or
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> if there are no more rows.
  </p>
  <p class="simpara">
   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
   <span class="function"><a href="function.mysql-fetch-row.php" class="function">mysql_fetch_row()</a></span> or add alias names.
   See the example at the <span class="function"><a href="function.mysql-fetch-array.php" class="function">mysql_fetch_array()</a></span>
   description about aliases.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-fetch-assoc-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 An expanded <span class="function"><strong>mysql_fetch_assoc()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"mysql_user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"mysql_password"</span><span style="color: #007700">);<br /><br />if (!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"Unable to connect to DB: " </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">();<br />    exit;<br />}<br /><br />if (!</span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"mydbname"</span><span style="color: #007700">)) {<br />    echo </span><span style="color: #DD0000">"Unable to select mydbname: " </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">();<br />    exit;<br />}<br /><br /></span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT id as userid, fullname, userstatus<br />        FROM   sometable<br />        WHERE  userstatus = 1"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /><br />if (!</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"Could not successfully run query (</span><span style="color: #0000BB">$sql</span><span style="color: #DD0000">) from DB: " </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">();<br />    exit;<br />}<br /><br />if (</span><span style="color: #0000BB">mysql_num_rows</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">) == </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"No rows found, nothing to print so am exiting"</span><span style="color: #007700">;<br />    exit;<br />}<br /><br /></span><span style="color: #FF8000">// While a row of data exists, put that row in $row as an associative array<br />// Note: If you're expecting just one row, no need to use a loop<br />// Note: If you put extract($row); inside the following loop, you'll<br />//       then create $userid, $fullname, and $userstatus<br /></span><span style="color: #007700">while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br />    echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">"userid"</span><span style="color: #007700">];<br />    echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">"fullname"</span><span style="color: #007700">];<br />    echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">"userstatus"</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #0000BB">mysql_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mysql-fetch-assoc-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Performance</strong><br />
   <span class="simpara">
    An important thing to note is that using
    <span class="function"><strong>mysql_fetch_assoc()</strong></span> is <em>not
    significantly</em> slower than using
    <span class="function"><a href="function.mysql-fetch-row.php" class="function">mysql_fetch_row()</a></span>, while it
    provides a significant added value.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Field names returned by this function
are <em>case-sensitive</em>.</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function sets NULL fields to
the PHP <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> value.</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysql-fetch-assoc-seealso">
  <h3 class="title">See Also</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.mysql-fetch-row.php" class="function" rel="rdfs-seeAlso">mysql_fetch_row()</a> - Get a result row as an enumerated array</span></li>
   <li><span class="function"><a href="function.mysql-fetch-array.php" class="function" rel="rdfs-seeAlso">mysql_fetch_array()</a> - Fetch a result row as an associative array, a numeric array, or both</span></li>
   <li><span class="function"><a href="function.mysql-data-seek.php" class="function" rel="rdfs-seeAlso">mysql_data_seek()</a> - Move internal result pointer</span></li>
   <li><span class="function"><a href="function.mysql-query.php" class="function" rel="rdfs-seeAlso">mysql_query()</a> - Send a MySQL query</span></li>
   <li><span class="function"><a href="function.mysql-error.php" class="function" rel="rdfs-seeAlso">mysql_error()</a> - Returns the text of the error message from previous MySQL operation</span></li>
  </ul>
 </div>

</div><?php manual_footer($setup); ?>