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

103 lines
3.2 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.2 $ -->
<refentry id="function.mysqli-fetch-assoc">
<refnamediv>
<refname>mysqli_fetch_assoc</refname>
<refpurpose>Fetch a result row as an associative array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>mysqli_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>
The <function>mysqli_fetch_assoc</function> function is used to return an associative array
representing the next row in the result set for the result represented by the
<parameter>result</parameter> parameter, where each key in the array represents the name
of one of the result set's columns.
</para>
<para>
If two or more columns in the result set have the same column name, the associative array
returned by the <function>mysqli_fetch_assoc</function> function will contain the value of
the last column of that name. If you must work with result sets with this properity, the
<function>mysqli_fetch_row</function> should be used which returns an numerically-indexed
array instead.
</para>
<example>
<title>An expanded <function>mysqli_fetch_assoc</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$conn = mysqli_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysqli_error();
exit;
}
if (!mysqli_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysqli_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysqli_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysqli_error();
exit;
}
if (mysqli_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 = mysqli_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
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
-->