php-doc-en/reference/mysql/functions/mysql-fetch-assoc.xml
Jakub Vrana 3b1c8c1a2e See also (bug #30037)
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@168283 c90b9560-bf6c-de11-be94-00142212c4b1
2004-09-10 08:26:44 +00:00

128 lines
3.8 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.11 $ -->
<!-- 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>
&database.field-case;
&database.fetch-null;
<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>
<para>
See also
<function>mysql_fetch_row</function>,
<function>mysql_fetch_array</function>,
<function>mysql_data_seek</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
-->